1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
13 use error::Error as StdError;
14 use ffi::{OsString, OsStr};
17 use path::{self, PathBuf};
19 use sys::{unsupported, Void, sgx_ineffective, decode_error_kind};
20 use collections::HashMap;
23 use sync::atomic::{AtomicUsize, Ordering};
26 pub fn errno() -> i32 {
30 pub fn error_string(errno: i32) -> String {
31 if errno == RESULT_SUCCESS {
32 "operation succesful".into()
33 } else if ((Error::UserRangeStart as _)..=(Error::UserRangeEnd as _)).contains(&errno) {
34 format!("user-specified error {:08x}", errno)
36 decode_error_kind(errno).as_str().into()
40 pub fn getcwd() -> io::Result<PathBuf> {
44 pub fn chdir(_: &path::Path) -> io::Result<()> {
48 pub struct SplitPaths<'a>(&'a Void);
50 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths {
54 impl<'a> Iterator for SplitPaths<'a> {
56 fn next(&mut self) -> Option<PathBuf> {
62 pub struct JoinPathsError;
64 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
65 where I: Iterator<Item=T>, T: AsRef<OsStr>
70 impl fmt::Display for JoinPathsError {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 "not supported in SGX yet".fmt(f)
76 impl StdError for JoinPathsError {
77 fn description(&self) -> &str {
78 "not supported in SGX yet"
82 pub fn current_exe() -> io::Result<PathBuf> {
86 static ENV: AtomicUsize = AtomicUsize::new(0);
87 static ENV_INIT: Once = Once::new();
88 type EnvStore = Mutex<HashMap<OsString, OsString>>;
90 fn get_env_store() -> Option<&'static EnvStore> {
91 unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
94 fn create_env_store() -> &'static EnvStore {
95 ENV_INIT.call_once(|| {
96 ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
99 &*(ENV.load(Ordering::Relaxed) as *const EnvStore)
103 pub type Env = vec::IntoIter<(OsString, OsString)>;
105 pub fn env() -> Env {
106 let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
107 map.iter().map(|(k, v)| (k.clone(), v.clone()) ).collect()
111 .map(|env| clone_to_vec(&env.lock().unwrap()) )
116 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
117 Ok(get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned() ))
120 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
121 let (k, v) = (k.to_owned(), v.to_owned());
122 create_env_store().lock().unwrap().insert(k, v);
126 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
127 if let Some(env) = get_env_store() {
128 env.lock().unwrap().remove(k);
133 pub fn temp_dir() -> PathBuf {
134 panic!("no filesystem in SGX")
137 pub fn home_dir() -> Option<PathBuf> {
141 pub fn exit(code: i32) -> ! {
142 super::abi::exit_with_code(code as _)
145 pub fn getpid() -> u32 {
146 panic!("no pids in SGX")