]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/sgx/os.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / sgx / os.rs
CommitLineData
0731742a
XL
1use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
2
60c5eb7d 3use crate::collections::HashMap;
532ac7d7 4use crate::error::Error as StdError;
60c5eb7d 5use crate::ffi::{OsStr, OsString};
532ac7d7
XL
6use crate::fmt;
7use crate::io;
8use crate::path::{self, PathBuf};
9use crate::str;
532ac7d7 10use crate::sync::atomic::{AtomicUsize, Ordering};
60c5eb7d 11use crate::sync::Mutex;
532ac7d7 12use crate::sync::Once;
60c5eb7d
XL
13use crate::sys::{decode_error_kind, sgx_ineffective, unsupported, Void};
14use crate::vec;
0731742a
XL
15
16pub fn errno() -> i32 {
17 RESULT_SUCCESS
18}
19
20pub 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)
25 } else {
26 decode_error_kind(errno).as_str().into()
27 }
28}
29
30pub fn getcwd() -> io::Result<PathBuf> {
31 unsupported()
32}
33
34pub fn chdir(_: &path::Path) -> io::Result<()> {
35 sgx_ineffective(())
36}
37
38pub struct SplitPaths<'a>(&'a Void);
39
532ac7d7 40pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
0731742a
XL
41 panic!("unsupported")
42}
43
44impl<'a> Iterator for SplitPaths<'a> {
45 type Item = PathBuf;
46 fn next(&mut self) -> Option<PathBuf> {
47 match *self.0 {}
48 }
49}
50
51#[derive(Debug)]
52pub struct JoinPathsError;
53
54pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
60c5eb7d
XL
55where
56 I: Iterator<Item = T>,
57 T: AsRef<OsStr>,
0731742a
XL
58{
59 Err(JoinPathsError)
60}
61
62impl fmt::Display for JoinPathsError {
532ac7d7 63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
0731742a
XL
64 "not supported in SGX yet".fmt(f)
65 }
66}
67
68impl StdError for JoinPathsError {
69 fn description(&self) -> &str {
70 "not supported in SGX yet"
71 }
72}
73
74pub fn current_exe() -> io::Result<PathBuf> {
75 unsupported()
76}
77
532ac7d7
XL
78#[cfg_attr(test, linkage = "available_externally")]
79#[export_name = "_ZN16__rust_internals3std3sys3sgx2os3ENVE"]
0731742a 80static ENV: AtomicUsize = AtomicUsize::new(0);
532ac7d7
XL
81#[cfg_attr(test, linkage = "available_externally")]
82#[export_name = "_ZN16__rust_internals3std3sys3sgx2os8ENV_INITE"]
0731742a
XL
83static ENV_INIT: Once = Once::new();
84type EnvStore = Mutex<HashMap<OsString, OsString>>;
85
86fn get_env_store() -> Option<&'static EnvStore> {
87 unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
88}
89
90fn create_env_store() -> &'static EnvStore {
91 ENV_INIT.call_once(|| {
92 ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
93 });
60c5eb7d 94 unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
0731742a
XL
95}
96
97pub type Env = vec::IntoIter<(OsString, OsString)>;
98
99pub fn env() -> Env {
100 let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
60c5eb7d 101 map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
0731742a
XL
102 };
103
60c5eb7d 104 get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default().into_iter()
0731742a
XL
105}
106
107pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
60c5eb7d 108 Ok(get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned()))
0731742a
XL
109}
110
111pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
112 let (k, v) = (k.to_owned(), v.to_owned());
113 create_env_store().lock().unwrap().insert(k, v);
114 Ok(())
115}
116
117pub fn unsetenv(k: &OsStr) -> io::Result<()> {
118 if let Some(env) = get_env_store() {
119 env.lock().unwrap().remove(k);
120 }
121 Ok(())
122}
123
124pub fn temp_dir() -> PathBuf {
125 panic!("no filesystem in SGX")
126}
127
128pub fn home_dir() -> Option<PathBuf> {
129 None
130}
131
132pub fn exit(code: i32) -> ! {
133 super::abi::exit_with_code(code as _)
134}
135
136pub fn getpid() -> u32 {
137 panic!("no pids in SGX")
138}