]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/windows/os.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / windows / os.rs
1 //! Implementation of `std::os` functionality for Windows.
2
3 #![allow(nonstandard_style)]
4
5 use crate::os::windows::prelude::*;
6
7 use crate::error::Error as StdError;
8 use crate::ffi::{OsStr, OsString};
9 use crate::fmt;
10 use crate::io;
11 use crate::os::windows::ffi::EncodeWide;
12 use crate::path::{self, PathBuf};
13 use crate::ptr;
14 use crate::slice;
15 use crate::sys::{c, cvt};
16
17 use super::to_u16s;
18
19 pub fn errno() -> i32 {
20 unsafe { c::GetLastError() as i32 }
21 }
22
23 /// Gets a detailed string description for the given error number.
24 pub fn error_string(mut errnum: i32) -> String {
25 // This value is calculated from the macro
26 // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
27 let langId = 0x0800 as c::DWORD;
28
29 let mut buf = [0 as c::WCHAR; 2048];
30
31 unsafe {
32 let mut module = ptr::null_mut();
33 let mut flags = 0;
34
35 // NTSTATUS errors may be encoded as HRESULT, which may returned from
36 // GetLastError. For more information about Windows error codes, see
37 // `[MS-ERREF]`: https://msdn.microsoft.com/en-us/library/cc231198.aspx
38 if (errnum & c::FACILITY_NT_BIT as i32) != 0 {
39 // format according to https://support.microsoft.com/en-us/help/259693
40 const NTDLL_DLL: &[u16] = &[
41 'N' as _, 'T' as _, 'D' as _, 'L' as _, 'L' as _, '.' as _, 'D' as _, 'L' as _,
42 'L' as _, 0,
43 ];
44 module = c::GetModuleHandleW(NTDLL_DLL.as_ptr());
45
46 if module != ptr::null_mut() {
47 errnum ^= c::FACILITY_NT_BIT as i32;
48 flags = c::FORMAT_MESSAGE_FROM_HMODULE;
49 }
50 }
51
52 let res = c::FormatMessageW(
53 flags | c::FORMAT_MESSAGE_FROM_SYSTEM | c::FORMAT_MESSAGE_IGNORE_INSERTS,
54 module,
55 errnum as c::DWORD,
56 langId,
57 buf.as_mut_ptr(),
58 buf.len() as c::DWORD,
59 ptr::null(),
60 ) as usize;
61 if res == 0 {
62 // Sometimes FormatMessageW can fail e.g., system doesn't like langId,
63 let fm_err = errno();
64 return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
65 }
66
67 match String::from_utf16(&buf[..res]) {
68 Ok(mut msg) => {
69 // Trim trailing CRLF inserted by FormatMessageW
70 let len = msg.trim_end().len();
71 msg.truncate(len);
72 msg
73 }
74 Err(..) => format!(
75 "OS Error {} (FormatMessageW() returned \
76 invalid UTF-16)",
77 errnum
78 ),
79 }
80 }
81 }
82
83 pub struct Env {
84 base: c::LPWCH,
85 cur: c::LPWCH,
86 }
87
88 impl Iterator for Env {
89 type Item = (OsString, OsString);
90
91 fn next(&mut self) -> Option<(OsString, OsString)> {
92 loop {
93 unsafe {
94 if *self.cur == 0 {
95 return None;
96 }
97 let p = &*self.cur as *const u16;
98 let mut len = 0;
99 while *p.offset(len) != 0 {
100 len += 1;
101 }
102 let s = slice::from_raw_parts(p, len as usize);
103 self.cur = self.cur.offset(len + 1);
104
105 // Windows allows environment variables to start with an equals
106 // symbol (in any other position, this is the separator between
107 // variable name and value). Since`s` has at least length 1 at
108 // this point (because the empty string terminates the array of
109 // environment variables), we can safely slice.
110 let pos = match s[1..].iter().position(|&u| u == b'=' as u16).map(|p| p + 1) {
111 Some(p) => p,
112 None => continue,
113 };
114 return Some((
115 OsStringExt::from_wide(&s[..pos]),
116 OsStringExt::from_wide(&s[pos + 1..]),
117 ));
118 }
119 }
120 }
121 }
122
123 impl Drop for Env {
124 fn drop(&mut self) {
125 unsafe {
126 c::FreeEnvironmentStringsW(self.base);
127 }
128 }
129 }
130
131 pub fn env() -> Env {
132 unsafe {
133 let ch = c::GetEnvironmentStringsW();
134 if ch as usize == 0 {
135 panic!("failure getting env string from OS: {}", io::Error::last_os_error());
136 }
137 Env { base: ch, cur: ch }
138 }
139 }
140
141 pub struct SplitPaths<'a> {
142 data: EncodeWide<'a>,
143 must_yield: bool,
144 }
145
146 pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
147 SplitPaths { data: unparsed.encode_wide(), must_yield: true }
148 }
149
150 impl<'a> Iterator for SplitPaths<'a> {
151 type Item = PathBuf;
152 fn next(&mut self) -> Option<PathBuf> {
153 // On Windows, the PATH environment variable is semicolon separated.
154 // Double quotes are used as a way of introducing literal semicolons
155 // (since c:\some;dir is a valid Windows path). Double quotes are not
156 // themselves permitted in path names, so there is no way to escape a
157 // double quote. Quoted regions can appear in arbitrary locations, so
158 //
159 // c:\foo;c:\som"e;di"r;c:\bar
160 //
161 // Should parse as [c:\foo, c:\some;dir, c:\bar].
162 //
163 // (The above is based on testing; there is no clear reference available
164 // for the grammar.)
165
166 let must_yield = self.must_yield;
167 self.must_yield = false;
168
169 let mut in_progress = Vec::new();
170 let mut in_quote = false;
171 for b in self.data.by_ref() {
172 if b == '"' as u16 {
173 in_quote = !in_quote;
174 } else if b == ';' as u16 && !in_quote {
175 self.must_yield = true;
176 break;
177 } else {
178 in_progress.push(b)
179 }
180 }
181
182 if !must_yield && in_progress.is_empty() {
183 None
184 } else {
185 Some(super::os2path(&in_progress))
186 }
187 }
188 }
189
190 #[derive(Debug)]
191 pub struct JoinPathsError;
192
193 pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
194 where
195 I: Iterator<Item = T>,
196 T: AsRef<OsStr>,
197 {
198 let mut joined = Vec::new();
199 let sep = b';' as u16;
200
201 for (i, path) in paths.enumerate() {
202 let path = path.as_ref();
203 if i > 0 {
204 joined.push(sep)
205 }
206 let v = path.encode_wide().collect::<Vec<u16>>();
207 if v.contains(&(b'"' as u16)) {
208 return Err(JoinPathsError);
209 } else if v.contains(&sep) {
210 joined.push(b'"' as u16);
211 joined.extend_from_slice(&v[..]);
212 joined.push(b'"' as u16);
213 } else {
214 joined.extend_from_slice(&v[..]);
215 }
216 }
217
218 Ok(OsStringExt::from_wide(&joined[..]))
219 }
220
221 impl fmt::Display for JoinPathsError {
222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
223 "path segment contains `\"`".fmt(f)
224 }
225 }
226
227 impl StdError for JoinPathsError {
228 fn description(&self) -> &str {
229 "failed to join paths"
230 }
231 }
232
233 pub fn current_exe() -> io::Result<PathBuf> {
234 super::fill_utf16_buf(
235 |buf, sz| unsafe { c::GetModuleFileNameW(ptr::null_mut(), buf, sz) },
236 super::os2path,
237 )
238 }
239
240 pub fn getcwd() -> io::Result<PathBuf> {
241 super::fill_utf16_buf(|buf, sz| unsafe { c::GetCurrentDirectoryW(sz, buf) }, super::os2path)
242 }
243
244 pub fn chdir(p: &path::Path) -> io::Result<()> {
245 let p: &OsStr = p.as_ref();
246 let mut p = p.encode_wide().collect::<Vec<_>>();
247 p.push(0);
248
249 cvt(unsafe { c::SetCurrentDirectoryW(p.as_ptr()) }).map(|_| ())
250 }
251
252 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
253 let k = to_u16s(k)?;
254 let res = super::fill_utf16_buf(
255 |buf, sz| unsafe { c::GetEnvironmentVariableW(k.as_ptr(), buf, sz) },
256 |buf| OsStringExt::from_wide(buf),
257 );
258 match res {
259 Ok(value) => Ok(Some(value)),
260 Err(e) => {
261 if e.raw_os_error() == Some(c::ERROR_ENVVAR_NOT_FOUND as i32) {
262 Ok(None)
263 } else {
264 Err(e)
265 }
266 }
267 }
268 }
269
270 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
271 let k = to_u16s(k)?;
272 let v = to_u16s(v)?;
273
274 cvt(unsafe { c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr()) }).map(|_| ())
275 }
276
277 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
278 let v = to_u16s(n)?;
279 cvt(unsafe { c::SetEnvironmentVariableW(v.as_ptr(), ptr::null()) }).map(|_| ())
280 }
281
282 pub fn temp_dir() -> PathBuf {
283 super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPathW(sz, buf) }, super::os2path).unwrap()
284 }
285
286 #[cfg(not(target_vendor = "uwp"))]
287 fn home_dir_crt() -> Option<PathBuf> {
288 unsafe {
289 use crate::sys::handle::Handle;
290
291 let me = c::GetCurrentProcess();
292 let mut token = ptr::null_mut();
293 if c::OpenProcessToken(me, c::TOKEN_READ, &mut token) == 0 {
294 return None;
295 }
296 let _handle = Handle::new(token);
297 super::fill_utf16_buf(
298 |buf, mut sz| {
299 match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
300 0 if c::GetLastError() != c::ERROR_INSUFFICIENT_BUFFER => 0,
301 0 => sz,
302 _ => sz - 1, // sz includes the null terminator
303 }
304 },
305 super::os2path,
306 )
307 .ok()
308 }
309 }
310
311 #[cfg(target_vendor = "uwp")]
312 fn home_dir_crt() -> Option<PathBuf> {
313 None
314 }
315
316 pub fn home_dir() -> Option<PathBuf> {
317 crate::env::var_os("HOME")
318 .or_else(|| crate::env::var_os("USERPROFILE"))
319 .map(PathBuf::from)
320 .or_else(|| home_dir_crt())
321 }
322
323 pub fn exit(code: i32) -> ! {
324 unsafe { c::ExitProcess(code as c::UINT) }
325 }
326
327 pub fn getpid() -> u32 {
328 unsafe { c::GetCurrentProcessId() as u32 }
329 }
330
331 #[cfg(test)]
332 mod tests {
333 use crate::io::Error;
334 use crate::sys::c;
335
336 // tests `error_string` above
337 #[test]
338 fn ntstatus_error() {
339 const STATUS_UNSUCCESSFUL: u32 = 0xc000_0001;
340 assert!(
341 !Error::from_raw_os_error((STATUS_UNSUCCESSFUL | c::FACILITY_NT_BIT) as _)
342 .to_string()
343 .contains("FormatMessageW() returned error")
344 );
345 }
346 }