]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/os.rs
Rollup merge of #105661 - lcnr:evaluate-new, r=compiler-errors
[rust.git] / library / std / src / sys / wasi / os.rs
1 #![deny(unsafe_op_in_unsafe_fn)]
2
3 use crate::error::Error as StdError;
4 use crate::ffi::{CStr, OsStr, OsString};
5 use crate::fmt;
6 use crate::io;
7 use crate::marker::PhantomData;
8 use crate::ops::Drop;
9 use crate::os::wasi::prelude::*;
10 use crate::path::{self, PathBuf};
11 use crate::str;
12 use crate::sys::common::small_c_string::{run_path_with_cstr, run_with_cstr};
13 use crate::sys::memchr;
14 use crate::sys::unsupported;
15 use crate::vec;
16
17 // Add a few symbols not in upstream `libc` just yet.
18 mod libc {
19     pub use libc::*;
20
21     extern "C" {
22         pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char;
23         pub fn chdir(dir: *const c_char) -> c_int;
24     }
25 }
26
27 cfg_if::cfg_if! {
28     if #[cfg(target_feature = "atomics")] {
29         // Access to the environment must be protected by a lock in multi-threaded scenarios.
30         use crate::sync::{PoisonError, RwLock};
31         static ENV_LOCK: RwLock<()> = RwLock::new(());
32         pub fn env_read_lock() -> impl Drop {
33             ENV_LOCK.read().unwrap_or_else(PoisonError::into_inner)
34         }
35         pub fn env_write_lock() -> impl Drop {
36             ENV_LOCK.write().unwrap_or_else(PoisonError::into_inner)
37         }
38     } else {
39         // No need for a lock if we are single-threaded.
40         pub fn env_read_lock() -> impl Drop {
41             Box::new(())
42         }
43         pub fn env_write_lock() -> impl Drop {
44             Box::new(())
45         }
46     }
47 }
48
49 pub fn errno() -> i32 {
50     extern "C" {
51         #[thread_local]
52         static errno: libc::c_int;
53     }
54
55     unsafe { errno as i32 }
56 }
57
58 pub fn error_string(errno: i32) -> String {
59     let mut buf = [0 as libc::c_char; 1024];
60
61     let p = buf.as_mut_ptr();
62     unsafe {
63         if libc::strerror_r(errno as libc::c_int, p, buf.len()) < 0 {
64             panic!("strerror_r failure");
65         }
66         str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
67     }
68 }
69
70 pub fn getcwd() -> io::Result<PathBuf> {
71     let mut buf = Vec::with_capacity(512);
72     loop {
73         unsafe {
74             let ptr = buf.as_mut_ptr() as *mut libc::c_char;
75             if !libc::getcwd(ptr, buf.capacity()).is_null() {
76                 let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
77                 buf.set_len(len);
78                 buf.shrink_to_fit();
79                 return Ok(PathBuf::from(OsString::from_vec(buf)));
80             } else {
81                 let error = io::Error::last_os_error();
82                 if error.raw_os_error() != Some(libc::ERANGE) {
83                     return Err(error);
84                 }
85             }
86
87             // Trigger the internal buffer resizing logic of `Vec` by requiring
88             // more space than the current capacity.
89             let cap = buf.capacity();
90             buf.set_len(cap);
91             buf.reserve(1);
92         }
93     }
94 }
95
96 pub fn chdir(p: &path::Path) -> io::Result<()> {
97     let result = run_path_with_cstr(p, |p| unsafe { Ok(libc::chdir(p.as_ptr())) })?;
98     match result == (0 as libc::c_int) {
99         true => Ok(()),
100         false => Err(io::Error::last_os_error()),
101     }
102 }
103
104 pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
105
106 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
107     panic!("unsupported")
108 }
109
110 impl<'a> Iterator for SplitPaths<'a> {
111     type Item = PathBuf;
112     fn next(&mut self) -> Option<PathBuf> {
113         self.0
114     }
115 }
116
117 #[derive(Debug)]
118 pub struct JoinPathsError;
119
120 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
121 where
122     I: Iterator<Item = T>,
123     T: AsRef<OsStr>,
124 {
125     Err(JoinPathsError)
126 }
127
128 impl fmt::Display for JoinPathsError {
129     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130         "not supported on wasm yet".fmt(f)
131     }
132 }
133
134 impl StdError for JoinPathsError {
135     #[allow(deprecated)]
136     fn description(&self) -> &str {
137         "not supported on wasm yet"
138     }
139 }
140
141 pub fn current_exe() -> io::Result<PathBuf> {
142     unsupported()
143 }
144 pub struct Env {
145     iter: vec::IntoIter<(OsString, OsString)>,
146 }
147
148 impl !Send for Env {}
149 impl !Sync for Env {}
150
151 impl Iterator for Env {
152     type Item = (OsString, OsString);
153     fn next(&mut self) -> Option<(OsString, OsString)> {
154         self.iter.next()
155     }
156     fn size_hint(&self) -> (usize, Option<usize>) {
157         self.iter.size_hint()
158     }
159 }
160
161 pub fn env() -> Env {
162     unsafe {
163         let _guard = env_read_lock();
164         let mut environ = libc::environ;
165         let mut result = Vec::new();
166         if !environ.is_null() {
167             while !(*environ).is_null() {
168                 if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
169                     result.push(key_value);
170                 }
171                 environ = environ.add(1);
172             }
173         }
174         return Env { iter: result.into_iter() };
175     }
176
177     // See src/libstd/sys/unix/os.rs, same as that
178     fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
179         if input.is_empty() {
180             return None;
181         }
182         let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
183         pos.map(|p| {
184             (
185                 OsStringExt::from_vec(input[..p].to_vec()),
186                 OsStringExt::from_vec(input[p + 1..].to_vec()),
187             )
188         })
189     }
190 }
191
192 pub fn getenv(k: &OsStr) -> Option<OsString> {
193     let s = run_with_cstr(k.as_bytes(), |k| unsafe {
194         let _guard = env_read_lock();
195         Ok(libc::getenv(k.as_ptr()) as *const libc::c_char)
196     })
197     .ok()?;
198     if s.is_null() {
199         None
200     } else {
201         Some(OsStringExt::from_vec(unsafe { CStr::from_ptr(s) }.to_bytes().to_vec()))
202     }
203 }
204
205 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
206     run_with_cstr(k.as_bytes(), |k| {
207         run_with_cstr(v.as_bytes(), |v| unsafe {
208             let _guard = env_write_lock();
209             cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
210         })
211     })
212 }
213
214 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
215     run_with_cstr(n.as_bytes(), |nbuf| unsafe {
216         let _guard = env_write_lock();
217         cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
218     })
219 }
220
221 pub fn temp_dir() -> PathBuf {
222     panic!("no filesystem on wasm")
223 }
224
225 pub fn home_dir() -> Option<PathBuf> {
226     None
227 }
228
229 pub fn exit(code: i32) -> ! {
230     unsafe { libc::exit(code) }
231 }
232
233 pub fn getpid() -> u32 {
234     panic!("unsupported");
235 }
236
237 #[doc(hidden)]
238 pub trait IsMinusOne {
239     fn is_minus_one(&self) -> bool;
240 }
241
242 macro_rules! impl_is_minus_one {
243     ($($t:ident)*) => ($(impl IsMinusOne for $t {
244         fn is_minus_one(&self) -> bool {
245             *self == -1
246         }
247     })*)
248 }
249
250 impl_is_minus_one! { i8 i16 i32 i64 isize }
251
252 fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
253     if t.is_minus_one() { Err(io::Error::last_os_error()) } else { Ok(t) }
254 }