diff options
| author | bors <bors@rust-lang.org> | 2019-12-25 00:41:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-12-25 00:41:55 +0000 |
| commit | c5a2a9a99c3973f77d7c86acb8ff7039c3d9c703 (patch) | |
| tree | 204c413efe7aef81a6a921f2cebfdb17817dae94 /src/libstd/sys/unix/os.rs | |
| parent | a5bc786399405c03a746ad385e579542788c8522 (diff) | |
| parent | 8d6d0e71a66355a9e2da366d22c7a4e355718fdf (diff) | |
| download | rust-c5a2a9a99c3973f77d7c86acb8ff7039c3d9c703.tar.gz rust-c5a2a9a99c3973f77d7c86acb8ff7039c3d9c703.zip | |
Auto merge of #67596 - Mark-Simulacrum:tidy-silence-rustfmt, r=Centril
(Mostly) finish formatting the repository Silences tidy line length warnings on rustfmt-controlled files. This leaves two things out of formatting: CloudABI (mostly because it's not really ours to control, it's upstream code that's mostly generated), and tests. The latter is a hard problem and maybe not one to worry too much about, we rarely edit old tests and reformatting them as we go if possible isn't too bad (they're generally small).
Diffstat (limited to 'src/libstd/sys/unix/os.rs')
| -rw-r--r-- | src/libstd/sys/unix/os.rs | 313 |
1 files changed, 181 insertions, 132 deletions
diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 10cdb25999c..95be564b330 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -5,7 +5,7 @@ use crate::os::unix::prelude::*; use crate::error::Error as StdError; -use crate::ffi::{CString, CStr, OsString, OsStr}; +use crate::ffi::{CStr, CString, OsStr, OsString}; use crate::fmt; use crate::io; use crate::iter; @@ -16,12 +16,12 @@ use crate::path::{self, PathBuf}; use crate::ptr; use crate::slice; use crate::str; -use crate::sys_common::mutex::{Mutex, MutexGuard}; use crate::sys::cvt; use crate::sys::fd; +use crate::sys_common::mutex::{Mutex, MutexGuard}; use crate::vec; -use libc::{c_int, c_char, c_void}; +use libc::{c_char, c_int, c_void}; const TMPBUF_SZ: usize = 128; @@ -33,24 +33,32 @@ cfg_if::cfg_if! { } } -extern { +extern "C" { #[cfg(not(target_os = "dragonfly"))] - #[cfg_attr(any(target_os = "linux", - target_os = "emscripten", - target_os = "fuchsia", - target_os = "l4re"), - link_name = "__errno_location")] - #[cfg_attr(any(target_os = "netbsd", - target_os = "openbsd", - target_os = "android", - target_os = "redox", - target_env = "newlib"), - link_name = "__errno")] + #[cfg_attr( + any( + target_os = "linux", + target_os = "emscripten", + target_os = "fuchsia", + target_os = "l4re" + ), + link_name = "__errno_location" + )] + #[cfg_attr( + any( + target_os = "netbsd", + target_os = "openbsd", + target_os = "android", + target_os = "redox", + target_env = "newlib" + ), + link_name = "__errno" + )] #[cfg_attr(target_os = "solaris", link_name = "___errno")] - #[cfg_attr(any(target_os = "macos", - target_os = "ios", - target_os = "freebsd"), - link_name = "__error")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios", target_os = "freebsd"), + link_name = "__error" + )] #[cfg_attr(target_os = "haiku", link_name = "_errnop")] fn errno_location() -> *mut c_int; } @@ -58,23 +66,18 @@ extern { /// Returns the platform-specific value of errno #[cfg(not(target_os = "dragonfly"))] pub fn errno() -> i32 { - unsafe { - (*errno_location()) as i32 - } + unsafe { (*errno_location()) as i32 } } /// Sets the platform-specific value of errno -#[cfg(all(not(target_os = "linux"), - not(target_os = "dragonfly")))] // needed for readdir and syscall! +#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly")))] // needed for readdir and syscall! pub fn set_errno(e: i32) { - unsafe { - *errno_location() = e as c_int - } + unsafe { *errno_location() = e as c_int } } #[cfg(target_os = "dragonfly")] pub fn errno() -> i32 { - extern { + extern "C" { #[thread_local] static errno: c_int; } @@ -84,7 +87,7 @@ pub fn errno() -> i32 { #[cfg(target_os = "dragonfly")] pub fn set_errno(e: i32) { - extern { + extern "C" { #[thread_local] static mut errno: c_int; } @@ -96,11 +99,9 @@ pub fn set_errno(e: i32) { /// Gets a detailed string description for the given error number. pub fn error_string(errno: i32) -> String { - extern { - #[cfg_attr(any(target_os = "linux", target_env = "newlib"), - link_name = "__xpg_strerror_r")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; + extern "C" { + #[cfg_attr(any(target_os = "linux", target_env = "newlib"), link_name = "__xpg_strerror_r")] + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t) -> c_int; } let mut buf = [0 as c_char; TMPBUF_SZ]; @@ -154,41 +155,51 @@ pub fn chdir(p: &path::Path) -> io::Result<()> { } pub struct SplitPaths<'a> { - iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, - fn(&'a [u8]) -> PathBuf>, + iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, fn(&'a [u8]) -> PathBuf>, } pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> { fn bytes_to_path(b: &[u8]) -> PathBuf { PathBuf::from(<OsStr as OsStrExt>::from_bytes(b)) } - fn is_separator(b: &u8) -> bool { *b == PATH_SEPARATOR } + fn is_separator(b: &u8) -> bool { + *b == PATH_SEPARATOR + } let unparsed = unparsed.as_bytes(); SplitPaths { - iter: unparsed.split(is_separator as fn(&u8) -> bool) - .map(bytes_to_path as fn(&[u8]) -> PathBuf) + iter: unparsed + .split(is_separator as fn(&u8) -> bool) + .map(bytes_to_path as fn(&[u8]) -> PathBuf), } } impl<'a> Iterator for SplitPaths<'a> { type Item = PathBuf; - fn next(&mut self) -> Option<PathBuf> { self.iter.next() } - fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } + fn next(&mut self) -> Option<PathBuf> { + self.iter.next() + } + fn size_hint(&self) -> (usize, Option<usize>) { + self.iter.size_hint() + } } #[derive(Debug)] pub struct JoinPathsError; pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError> - where I: Iterator<Item=T>, T: AsRef<OsStr> +where + I: Iterator<Item = T>, + T: AsRef<OsStr>, { let mut joined = Vec::new(); for (i, path) in paths.enumerate() { let path = path.as_ref().as_bytes(); - if i > 0 { joined.push(PATH_SEPARATOR) } + if i > 0 { + joined.push(PATH_SEPARATOR) + } if path.contains(&PATH_SEPARATOR) { - return Err(JoinPathsError) + return Err(JoinPathsError); } joined.extend_from_slice(path); } @@ -202,26 +213,41 @@ impl fmt::Display for JoinPathsError { } impl StdError for JoinPathsError { - fn description(&self) -> &str { "failed to join paths" } + fn description(&self) -> &str { + "failed to join paths" + } } #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] pub fn current_exe() -> io::Result<PathBuf> { unsafe { - let mut mib = [libc::CTL_KERN as c_int, - libc::KERN_PROC as c_int, - libc::KERN_PROC_PATHNAME as c_int, - -1 as c_int]; + let mut mib = [ + libc::CTL_KERN as c_int, + libc::KERN_PROC as c_int, + libc::KERN_PROC_PATHNAME as c_int, + -1 as c_int, + ]; let mut sz = 0; - cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as libc::c_uint, - ptr::null_mut(), &mut sz, ptr::null_mut(), 0))?; + cvt(libc::sysctl( + mib.as_mut_ptr(), + mib.len() as libc::c_uint, + ptr::null_mut(), + &mut sz, + ptr::null_mut(), + 0, + ))?; if sz == 0 { - return Err(io::Error::last_os_error()) + return Err(io::Error::last_os_error()); } let mut v: Vec<u8> = Vec::with_capacity(sz); - cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as libc::c_uint, - v.as_mut_ptr() as *mut libc::c_void, &mut sz, - ptr::null_mut(), 0))?; + cvt(libc::sysctl( + mib.as_mut_ptr(), + mib.len() as libc::c_uint, + v.as_mut_ptr() as *mut libc::c_void, + &mut sz, + ptr::null_mut(), + 0, + ))?; if sz == 0 { return Err(io::Error::last_os_error()); } @@ -236,17 +262,29 @@ pub fn current_exe() -> io::Result<PathBuf> { unsafe { let mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, -1, libc::KERN_PROC_PATHNAME]; let mut path_len: usize = 0; - cvt(libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint, - ptr::null_mut(), &mut path_len, - ptr::null(), 0))?; + cvt(libc::sysctl( + mib.as_ptr(), + mib.len() as libc::c_uint, + ptr::null_mut(), + &mut path_len, + ptr::null(), + 0, + ))?; if path_len <= 1 { - return Err(io::Error::new(io::ErrorKind::Other, - "KERN_PROC_PATHNAME sysctl returned zero-length string")) + return Err(io::Error::new( + io::ErrorKind::Other, + "KERN_PROC_PATHNAME sysctl returned zero-length string", + )); } let mut path: Vec<u8> = Vec::with_capacity(path_len); - cvt(libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint, - path.as_ptr() as *mut libc::c_void, &mut path_len, - ptr::null(), 0))?; + cvt(libc::sysctl( + mib.as_ptr(), + mib.len() as libc::c_uint, + path.as_ptr() as *mut libc::c_void, + &mut path_len, + ptr::null(), + 0, + ))?; path.set_len(path_len - 1); // chop off NUL Ok(PathBuf::from(OsString::from_vec(path))) } @@ -256,8 +294,10 @@ pub fn current_exe() -> io::Result<PathBuf> { if curproc_exe.is_file() { return crate::fs::read_link(curproc_exe); } - Err(io::Error::new(io::ErrorKind::Other, - "/proc/curproc/exe doesn't point to regular file.")) + Err(io::Error::new( + io::ErrorKind::Other, + "/proc/curproc/exe doesn't point to regular file.", + )) } sysctl().or_else(|_| procfs()) } @@ -265,21 +305,15 @@ pub fn current_exe() -> io::Result<PathBuf> { #[cfg(target_os = "openbsd")] pub fn current_exe() -> io::Result<PathBuf> { unsafe { - let mut mib = [libc::CTL_KERN, - libc::KERN_PROC_ARGS, - libc::getpid(), - libc::KERN_PROC_ARGV]; + let mut mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, libc::getpid(), libc::KERN_PROC_ARGV]; let mib = mib.as_mut_ptr(); let mut argv_len = 0; - cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len, - ptr::null_mut(), 0))?; + cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len, ptr::null_mut(), 0))?; let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize); - cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _, - &mut argv_len, ptr::null_mut(), 0))?; + cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _, &mut argv_len, ptr::null_mut(), 0))?; argv.set_len(argv_len as usize); if argv[0].is_null() { - return Err(io::Error::new(io::ErrorKind::Other, - "no current exe available")) + return Err(io::Error::new(io::ErrorKind::Other, "no current exe available")); } let argv0 = CStr::from_ptr(argv[0]).to_bytes(); if argv0[0] == b'.' || argv0.iter().any(|b| *b == b'/') { @@ -293,29 +327,30 @@ pub fn current_exe() -> io::Result<PathBuf> { #[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))] pub fn current_exe() -> io::Result<PathBuf> { match crate::fs::read_link("/proc/self/exe") { - Err(ref e) if e.kind() == io::ErrorKind::NotFound => { - Err(io::Error::new( - io::ErrorKind::Other, - "no /proc/self/exe available. Is /proc mounted?" - )) - }, + Err(ref e) if e.kind() == io::ErrorKind::NotFound => Err(io::Error::new( + io::ErrorKind::Other, + "no /proc/self/exe available. Is /proc mounted?", + )), other => other, } } #[cfg(any(target_os = "macos", target_os = "ios"))] pub fn current_exe() -> io::Result<PathBuf> { - extern { - fn _NSGetExecutablePath(buf: *mut libc::c_char, - bufsize: *mut u32) -> libc::c_int; + extern "C" { + fn _NSGetExecutablePath(buf: *mut libc::c_char, bufsize: *mut u32) -> libc::c_int; } unsafe { let mut sz: u32 = 0; _NSGetExecutablePath(ptr::null_mut(), &mut sz); - if sz == 0 { return Err(io::Error::last_os_error()); } + if sz == 0 { + return Err(io::Error::last_os_error()); + } let mut v: Vec<u8> = Vec::with_capacity(sz as usize); let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz); - if err != 0 { return Err(io::Error::last_os_error()); } + if err != 0 { + return Err(io::Error::last_os_error()); + } v.set_len(sz as usize - 1); // chop off trailing NUL Ok(PathBuf::from(OsString::from_vec(v))) } @@ -323,7 +358,7 @@ pub fn current_exe() -> io::Result<PathBuf> { #[cfg(any(target_os = "solaris"))] pub fn current_exe() -> io::Result<PathBuf> { - extern { + extern "C" { fn getexecname() -> *const c_char; } unsafe { @@ -336,11 +371,7 @@ pub fn current_exe() -> io::Result<PathBuf> { // Prepend a current working directory to the path if // it doesn't contain an absolute pathname. - if filename[0] == b'/' { - Ok(path) - } else { - getcwd().map(|cwd| cwd.join(path)) - } + if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) } } } } @@ -354,11 +385,11 @@ pub fn current_exe() -> io::Result<PathBuf> { type_: i32, sequence: i32, init_order: i32, - init_routine: *mut libc::c_void, // function pointer - term_routine: *mut libc::c_void, // function pointer + init_routine: *mut libc::c_void, // function pointer + term_routine: *mut libc::c_void, // function pointer device: libc::dev_t, node: libc::ino_t, - name: [libc::c_char; 1024], // MAXPATHLEN + name: [libc::c_char; 1024], // MAXPATHLEN text: *mut libc::c_void, data: *mut libc::c_void, text_size: i32, @@ -368,16 +399,20 @@ pub fn current_exe() -> io::Result<PathBuf> { } unsafe { - extern { - fn _get_next_image_info(team_id: i32, cookie: *mut i32, - info: *mut image_info, size: i32) -> i32; + extern "C" { + fn _get_next_image_info( + team_id: i32, + cookie: *mut i32, + info: *mut image_info, + size: i32, + ) -> i32; } let mut info: image_info = mem::zeroed(); let mut cookie: i32 = 0; // the executable can be found at team id 0 - let result = _get_next_image_info(0, &mut cookie, &mut info, - mem::size_of::<image_info>() as i32); + let result = + _get_next_image_info(0, &mut cookie, &mut info, mem::size_of::<image_info>() as i32); if result != 0 { use crate::io::ErrorKind; Err(io::Error::new(ErrorKind::Other, "Error getting executable path")) @@ -406,19 +441,27 @@ pub struct Env { impl Iterator for Env { type Item = (OsString, OsString); - fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() } - fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } + fn next(&mut self) -> Option<(OsString, OsString)> { + self.iter.next() + } + fn size_hint(&self) -> (usize, Option<usize>) { + self.iter.size_hint() + } } #[cfg(target_os = "macos")] pub unsafe fn environ() -> *mut *const *const c_char { - extern { fn _NSGetEnviron() -> *mut *const *const c_char; } + extern "C" { + fn _NSGetEnviron() -> *mut *const *const c_char; + } _NSGetEnviron() } #[cfg(not(target_os = "macos"))] pub unsafe fn environ() -> *mut *const *const c_char { - extern { static mut environ: *const *const c_char; } + extern "C" { + static mut environ: *const *const c_char; + } &mut environ } @@ -442,10 +485,7 @@ pub fn env() -> Env { } environ = environ.offset(1); } - return Env { - iter: result.into_iter(), - _dont_send_or_sync_me: PhantomData, - } + return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData }; } fn parse(input: &[u8]) -> Option<(OsString, OsString)> { @@ -457,10 +497,12 @@ pub fn env() -> Env { return None; } let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1); - pos.map(|p| ( - OsStringExt::from_vec(input[..p].to_vec()), - OsStringExt::from_vec(input[p+1..].to_vec()), - )) + pos.map(|p| { + ( + OsStringExt::from_vec(input[..p].to_vec()), + OsStringExt::from_vec(input[p + 1..].to_vec()), + ) + }) } } @@ -500,9 +542,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> { } pub fn page_size() -> usize { - unsafe { - libc::sysconf(libc::_SC_PAGESIZE) as usize - } + unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize } } pub fn temp_dir() -> PathBuf { @@ -516,19 +556,23 @@ pub fn temp_dir() -> PathBuf { } pub fn home_dir() -> Option<PathBuf> { - return crate::env::var_os("HOME").or_else(|| unsafe { - fallback() - }).map(PathBuf::from); - - #[cfg(any(target_os = "android", - target_os = "ios", - target_os = "emscripten", - target_os = "redox"))] - unsafe fn fallback() -> Option<OsString> { None } - #[cfg(not(any(target_os = "android", - target_os = "ios", - target_os = "emscripten", - target_os = "redox")))] + return crate::env::var_os("HOME").or_else(|| unsafe { fallback() }).map(PathBuf::from); + + #[cfg(any( + target_os = "android", + target_os = "ios", + target_os = "emscripten", + target_os = "redox" + ))] + unsafe fn fallback() -> Option<OsString> { + None + } + #[cfg(not(any( + target_os = "android", + target_os = "ios", + target_os = "emscripten", + target_os = "redox" + )))] unsafe fn fallback() -> Option<OsString> { let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) { n if n < 0 => 512 as usize, @@ -537,13 +581,18 @@ pub fn home_dir() -> Option<PathBuf> { let mut buf = Vec::with_capacity(amt); let mut passwd: libc::passwd = mem::zeroed(); let mut result = ptr::null_mut(); - match libc::getpwuid_r(libc::getuid(), &mut passwd, buf.as_mut_ptr(), - buf.capacity(), &mut result) { + match libc::getpwuid_r( + libc::getuid(), + &mut passwd, + buf.as_mut_ptr(), + buf.capacity(), + &mut result, + ) { 0 if !result.is_null() => { let ptr = passwd.pw_dir as *const _; let bytes = CStr::from_ptr(ptr).to_bytes().to_vec(); Some(OsStringExt::from_vec(bytes)) - }, + } _ => None, } } @@ -589,7 +638,7 @@ fn parse_glibc_version(version: &str) -> Option<(usize, usize)> { let mut parsed_ints = version.split('.').map(str::parse::<usize>).fuse(); match (parsed_ints.next(), parsed_ints.next()) { (Some(Ok(major)), Some(Ok(minor))) => Some((major, minor)), - _ => None + _ => None, } } |
