1 use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
3 use crate::error::Error as StdError;
4 use crate::ffi::{OsString, OsStr};
7 use crate::path::{self, PathBuf};
9 use crate::sys::{unsupported, Void, sgx_ineffective, decode_error_kind};
10 use crate::collections::HashMap;
12 use crate::sync::Mutex;
13 use crate::sync::atomic::{AtomicUsize, Ordering};
14 use crate::sync::Once;
16 pub fn errno() -> i32 {
20 pub fn error_string(errno: i32) -> String {
21 if errno == RESULT_SUCCESS {
22 "operation succesful".into()
23 } else if ((Error::UserRangeStart as _)..=(Error::UserRangeEnd as _)).contains(&errno) {
24 format!("user-specified error {:08x}", errno)
26 decode_error_kind(errno).as_str().into()
30 pub fn getcwd() -> io::Result<PathBuf> {
34 pub fn chdir(_: &path::Path) -> io::Result<()> {
38 pub struct SplitPaths<'a>(&'a Void);
40 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths {
44 impl<'a> Iterator for SplitPaths<'a> {
46 fn next(&mut self) -> Option<PathBuf> {
52 pub struct JoinPathsError;
54 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
55 where I: Iterator<Item=T>, T: AsRef<OsStr>
60 impl fmt::Display for JoinPathsError {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 "not supported in SGX yet".fmt(f)
66 impl StdError for JoinPathsError {
67 fn description(&self) -> &str {
68 "not supported in SGX yet"
72 pub fn current_exe() -> io::Result<PathBuf> {
76 static ENV: AtomicUsize = AtomicUsize::new(0);
77 static ENV_INIT: Once = Once::new();
78 type EnvStore = Mutex<HashMap<OsString, OsString>>;
80 fn get_env_store() -> Option<&'static EnvStore> {
81 unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
84 fn create_env_store() -> &'static EnvStore {
85 ENV_INIT.call_once(|| {
86 ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
89 &*(ENV.load(Ordering::Relaxed) as *const EnvStore)
93 pub type Env = vec::IntoIter<(OsString, OsString)>;
96 let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
97 map.iter().map(|(k, v)| (k.clone(), v.clone()) ).collect()
101 .map(|env| clone_to_vec(&env.lock().unwrap()) )
106 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
107 Ok(get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned() ))
110 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
111 let (k, v) = (k.to_owned(), v.to_owned());
112 create_env_store().lock().unwrap().insert(k, v);
116 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
117 if let Some(env) = get_env_store() {
118 env.lock().unwrap().remove(k);
123 pub fn temp_dir() -> PathBuf {
124 panic!("no filesystem in SGX")
127 pub fn home_dir() -> Option<PathBuf> {
131 pub fn exit(code: i32) -> ! {
132 super::abi::exit_with_code(code as _)
135 pub fn getpid() -> u32 {
136 panic!("no pids in SGX")