]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/os.rs
rollup merge of #20348: frewsxcv/rm-reexports
[rust.git] / src / libstd / sys / windows / os.rs
1 // Copyright 2014 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.
4 //
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.
10
11 //! Implementation of `std::os` functionality for Windows
12
13 // FIXME: move various extern bindings from here into liblibc or
14 // something similar
15
16 use prelude::*;
17
18 use io::{IoResult, IoError};
19 use libc::{c_int, c_void};
20 use libc;
21 use os;
22 use path::BytesContainer;
23 use ptr;
24 use sys::fs::FileDesc;
25 use slice;
26
27 use os::TMPBUF_SZ;
28 use libc::types::os::arch::extra::DWORD;
29
30 const BUF_BYTES : uint = 2048u;
31
32 /// Return a slice of `v` ending at (and not including) the first NUL
33 /// (0).
34 pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
35     match v.iter().position(|c| *c == 0) {
36         // don't include the 0
37         Some(i) => v[..i],
38         None => v
39     }
40 }
41
42 pub fn errno() -> uint {
43     use libc::types::os::arch::extra::DWORD;
44
45     #[link_name = "kernel32"]
46     extern "system" {
47         fn GetLastError() -> DWORD;
48     }
49
50     unsafe {
51         GetLastError() as uint
52     }
53 }
54
55 /// Get a detailed string description for the given error number
56 pub fn error_string(errnum: i32) -> String {
57     use libc::types::os::arch::extra::DWORD;
58     use libc::types::os::arch::extra::LPWSTR;
59     use libc::types::os::arch::extra::LPVOID;
60     use libc::types::os::arch::extra::WCHAR;
61
62     #[link_name = "kernel32"]
63     extern "system" {
64         fn FormatMessageW(flags: DWORD,
65                           lpSrc: LPVOID,
66                           msgId: DWORD,
67                           langId: DWORD,
68                           buf: LPWSTR,
69                           nsize: DWORD,
70                           args: *const c_void)
71                           -> DWORD;
72     }
73
74     static FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
75     static FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
76
77     // This value is calculated from the macro
78     // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
79     let langId = 0x0800 as DWORD;
80
81     let mut buf = [0 as WCHAR, ..TMPBUF_SZ];
82
83     unsafe {
84         let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
85                                  FORMAT_MESSAGE_IGNORE_INSERTS,
86                                  ptr::null_mut(),
87                                  errnum as DWORD,
88                                  langId,
89                                  buf.as_mut_ptr(),
90                                  buf.len() as DWORD,
91                                  ptr::null());
92         if res == 0 {
93             // Sometimes FormatMessageW can fail e.g. system doesn't like langId,
94             let fm_err = errno();
95             return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
96         }
97
98         let msg = String::from_utf16(truncate_utf16_at_nul(&buf));
99         match msg {
100             Ok(msg) => format!("OS Error {}: {}", errnum, msg),
101             Err(..) => format!("OS Error {} (FormatMessageW() returned \
102                                 invalid UTF-16)", errnum),
103         }
104     }
105 }
106
107 pub unsafe fn pipe() -> IoResult<(FileDesc, FileDesc)> {
108     // Windows pipes work subtly differently than unix pipes, and their
109     // inheritance has to be handled in a different way that I do not
110     // fully understand. Here we explicitly make the pipe non-inheritable,
111     // which means to pass it to a subprocess they need to be duplicated
112     // first, as in std::run.
113     let mut fds = [0, ..2];
114     match libc::pipe(fds.as_mut_ptr(), 1024 as ::libc::c_uint,
115                      (libc::O_BINARY | libc::O_NOINHERIT) as c_int) {
116         0 => {
117             assert!(fds[0] != -1 && fds[0] != 0);
118             assert!(fds[1] != -1 && fds[1] != 0);
119             Ok((FileDesc::new(fds[0], true), FileDesc::new(fds[1], true)))
120         }
121         _ => Err(IoError::last_error()),
122     }
123 }
124
125 pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD) -> Option<String> {
126     unsafe {
127         let mut n = TMPBUF_SZ as DWORD;
128         let mut res = None;
129         let mut done = false;
130         while !done {
131             let mut buf = Vec::from_elem(n as uint, 0u16);
132             let k = f(buf.as_mut_ptr(), n);
133             if k == (0 as DWORD) {
134                 done = true;
135             } else if k == n &&
136                       libc::GetLastError() ==
137                       libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
138                 n *= 2 as DWORD;
139             } else if k >= n {
140                 n = k;
141             } else {
142                 done = true;
143             }
144             if k != 0 && done {
145                 let sub = buf.slice(0, k as uint);
146                 // We want to explicitly catch the case when the
147                 // closure returned invalid UTF-16, rather than
148                 // set `res` to None and continue.
149                 let s = String::from_utf16(sub).ok()
150                     .expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
151                 res = Some(s)
152             }
153         }
154         return res;
155     }
156 }
157
158 pub fn getcwd() -> IoResult<Path> {
159     use libc::DWORD;
160     use libc::GetCurrentDirectoryW;
161     use io::OtherIoError;
162
163     let mut buf = [0 as u16, ..BUF_BYTES];
164     unsafe {
165         if libc::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) == 0 as DWORD {
166             return Err(IoError::last_error());
167         }
168     }
169
170     match String::from_utf16(truncate_utf16_at_nul(&buf)) {
171         Ok(ref cwd) => Ok(Path::new(cwd)),
172         Err(..) => Err(IoError {
173             kind: OtherIoError,
174             desc: "GetCurrentDirectoryW returned invalid UTF-16",
175             detail: None,
176         }),
177     }
178 }
179
180 pub unsafe fn get_env_pairs() -> Vec<Vec<u8>> {
181     use libc::funcs::extra::kernel32::{
182         GetEnvironmentStringsW,
183         FreeEnvironmentStringsW
184     };
185     let ch = GetEnvironmentStringsW();
186     if ch as uint == 0 {
187         panic!("os::env() failure getting env string from OS: {}",
188                os::last_os_error());
189     }
190     // Here, we lossily decode the string as UTF16.
191     //
192     // The docs suggest that the result should be in Unicode, but
193     // Windows doesn't guarantee it's actually UTF16 -- it doesn't
194     // validate the environment string passed to CreateProcess nor
195     // SetEnvironmentVariable.  Yet, it's unlikely that returning a
196     // raw u16 buffer would be of practical use since the result would
197     // be inherently platform-dependent and introduce additional
198     // complexity to this code.
199     //
200     // Using the non-Unicode version of GetEnvironmentStrings is even
201     // worse since the result is in an OEM code page.  Characters that
202     // can't be encoded in the code page would be turned into question
203     // marks.
204     let mut result = Vec::new();
205     let mut i = 0;
206     while *ch.offset(i) != 0 {
207         let p = &*ch.offset(i);
208         let mut len = 0;
209         while *(p as *const _).offset(len) != 0 {
210             len += 1;
211         }
212         let p = p as *const u16;
213         let s = slice::from_raw_buf(&p, len as uint);
214         result.push(String::from_utf16_lossy(s).into_bytes());
215         i += len as int + 1;
216     }
217     FreeEnvironmentStringsW(ch);
218     result
219 }
220
221 pub fn split_paths(unparsed: &[u8]) -> Vec<Path> {
222     // On Windows, the PATH environment variable is semicolon separated.  Double
223     // quotes are used as a way of introducing literal semicolons (since
224     // c:\some;dir is a valid Windows path). Double quotes are not themselves
225     // permitted in path names, so there is no way to escape a double quote.
226     // Quoted regions can appear in arbitrary locations, so
227     //
228     //   c:\foo;c:\som"e;di"r;c:\bar
229     //
230     // Should parse as [c:\foo, c:\some;dir, c:\bar].
231     //
232     // (The above is based on testing; there is no clear reference available
233     // for the grammar.)
234
235     let mut parsed = Vec::new();
236     let mut in_progress = Vec::new();
237     let mut in_quote = false;
238
239     for b in unparsed.iter() {
240         match *b {
241             b';' if !in_quote => {
242                 parsed.push(Path::new(in_progress.as_slice()));
243                 in_progress.truncate(0)
244             }
245             b'"' => {
246                 in_quote = !in_quote;
247             }
248             _  => {
249                 in_progress.push(*b);
250             }
251         }
252     }
253     parsed.push(Path::new(in_progress));
254     parsed
255 }
256
257 pub fn join_paths<T: BytesContainer>(paths: &[T]) -> Result<Vec<u8>, &'static str> {
258     let mut joined = Vec::new();
259     let sep = b';';
260
261     for (i, path) in paths.iter().map(|p| p.container_as_bytes()).enumerate() {
262         if i > 0 { joined.push(sep) }
263         if path.contains(&b'"') {
264             return Err("path segment contains `\"`");
265         } else if path.contains(&sep) {
266             joined.push(b'"');
267             joined.push_all(path);
268             joined.push(b'"');
269         } else {
270             joined.push_all(path);
271         }
272     }
273
274     Ok(joined)
275 }
276
277 pub fn load_self() -> Option<Vec<u8>> {
278     unsafe {
279         fill_utf16_buf_and_decode(|buf, sz| {
280             libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
281         }).map(|s| s.to_string().into_bytes())
282     }
283 }
284
285 pub fn chdir(p: &Path) -> IoResult<()> {
286     let mut p = p.as_str().unwrap().utf16_units().collect::<Vec<u16>>();
287     p.push(0);
288
289     unsafe {
290         match libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL) {
291             true => Ok(()),
292             false => Err(IoError::last_error()),
293         }
294     }
295 }
296
297 pub fn page_size() -> uint {
298     use mem;
299     unsafe {
300         let mut info = mem::zeroed();
301         libc::GetSystemInfo(&mut info);
302
303         return info.dwPageSize as uint;
304     }
305 }
306
307 #[cfg(test)]
308 mod tests {
309     use super::truncate_utf16_at_nul;
310
311     #[test]
312     fn test_truncate_utf16_at_nul() {
313         let v = [];
314         let b: &[u16] = &[];
315         assert_eq!(truncate_utf16_at_nul(&v), b);
316
317         let v = [0, 2, 3];
318         assert_eq!(truncate_utf16_at_nul(&v), b);
319
320         let v = [1, 0, 3];
321         let b: &[u16] = &[1];
322         assert_eq!(truncate_utf16_at_nul(&v), b);
323
324         let v = [1, 2, 0];
325         let b: &[u16] = &[1, 2];
326         assert_eq!(truncate_utf16_at_nul(&v), b);
327
328         let v = [1, 2, 3];
329         let b: &[u16] = &[1, 2, 3];
330         assert_eq!(truncate_utf16_at_nul(&v), b);
331     }
332 }