use exhaustive_patterns to be able to use `?`

This commit is contained in:
Marcel Hellwig 2019-05-06 15:40:34 +02:00
parent af6ace6209
commit 5458b651b1
2 changed files with 15 additions and 12 deletions

View File

@ -150,7 +150,7 @@ impl Command {
match cvt(syscall::clone(0))? { match cvt(syscall::clone(0))? {
0 => { 0 => {
drop(input); drop(input);
let err = self.do_exec(theirs); let Err(err) = self.do_exec(theirs);
let errno = err.raw_os_error().unwrap_or(syscall::EINVAL) as u32; let errno = err.raw_os_error().unwrap_or(syscall::EINVAL) as u32;
let bytes = [ let bytes = [
(errno >> 24) as u8, (errno >> 24) as u8,
@ -218,7 +218,10 @@ impl Command {
} }
match self.setup_io(default, true) { match self.setup_io(default, true) {
Ok((_, theirs)) => unsafe { self.do_exec(theirs) }, Ok((_, theirs)) => unsafe {
let Err(e) = self.do_exec(theirs);
e
},
Err(e) => e, Err(e) => e,
} }
} }
@ -253,7 +256,7 @@ impl Command {
// allocation). Instead we just close it manually. This will never // allocation). Instead we just close it manually. This will never
// have the drop glue anyway because this code never returns (the // have the drop glue anyway because this code never returns (the
// child will either exec() or invoke syscall::exit) // child will either exec() or invoke syscall::exit)
unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Error { unsafe fn do_exec(&mut self, stdio: ChildPipes) -> Result<!, io::Error> {
if let Some(fd) = stdio.stderr.fd() { if let Some(fd) = stdio.stderr.fd() {
cvt(syscall::dup2(fd, 2, &[]))?; cvt(syscall::dup2(fd, 2, &[]))?;
let mut flags = cvt(syscall::fcntl(2, syscall::F_GETFD, 0))?; let mut flags = cvt(syscall::fcntl(2, syscall::F_GETFD, 0))?;
@ -308,7 +311,7 @@ impl Command {
let mut file = if let Some(program) = program { let mut file = if let Some(program) = program {
File::open(program.as_os_str())? File::open(program.as_os_str())?
} else { } else {
return io::Error::from_raw_os_error(syscall::ENOENT); return Err(io::Error::from_raw_os_error(syscall::ENOENT));
}; };
// Push all the arguments // Push all the arguments
@ -343,7 +346,7 @@ impl Command {
meta.mode() & 0o7 meta.mode() & 0o7
}; };
if mode & 1 == 0 { if mode & 1 == 0 {
return io::Error::from_raw_os_error(syscall::EPERM); return Err(io::Error::from_raw_os_error(syscall::EPERM));
} }
// Second of all, we need to actually read which interpreter it wants // Second of all, we need to actually read which interpreter it wants
@ -389,13 +392,12 @@ impl Command {
} }
if let Err(err) = syscall::fexec(file.as_raw_fd(), &args, &vars) { if let Err(err) = syscall::fexec(file.as_raw_fd(), &args, &vars) {
io::Error::from_raw_os_error(err.errno as i32) Err(io::Error::from_raw_os_error(err.errno as i32))
} else { } else {
panic!("return from exec without err"); panic!("return from exec without err");
} }
} }
fn setup_io(&self, default: Stdio, needs_stdin: bool) fn setup_io(&self, default: Stdio, needs_stdin: bool)
-> io::Result<(StdioPipes, ChildPipes)> { -> io::Result<(StdioPipes, ChildPipes)> {
let null = Stdio::Null; let null = Stdio::Null;

View File

@ -47,7 +47,7 @@ impl Command {
match result { match result {
0 => { 0 => {
drop(input); drop(input);
let err = self.do_exec(theirs, envp.as_ref()); let Err(err) = self.do_exec(theirs, envp.as_ref());
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32; let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
let bytes = [ let bytes = [
(errno >> 24) as u8, (errno >> 24) as u8,
@ -123,7 +123,8 @@ impl Command {
// environment lock before we try to exec. // environment lock before we try to exec.
let _lock = sys::os::env_lock(); let _lock = sys::os::env_lock();
self.do_exec(theirs, envp.as_ref()) let Err(e) = self.do_exec(theirs, envp.as_ref());
e
} }
} }
Err(e) => e, Err(e) => e,
@ -164,7 +165,7 @@ impl Command {
&mut self, &mut self,
stdio: ChildPipes, stdio: ChildPipes,
maybe_envp: Option<&CStringArray> maybe_envp: Option<&CStringArray>
) -> io::Error { ) -> Result<!, io::Error> {
use crate::sys::{self, cvt_r}; use crate::sys::{self, cvt_r};
if let Some(fd) = stdio.stdin.fd() { if let Some(fd) = stdio.stdin.fd() {
@ -224,7 +225,7 @@ impl Command {
ptr::null_mut()))?; ptr::null_mut()))?;
let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL); let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
if ret == libc::SIG_ERR { if ret == libc::SIG_ERR {
return io::Error::last_os_error() return Err(io::Error::last_os_error())
} }
} }
@ -254,7 +255,7 @@ impl Command {
} }
libc::execvp(self.get_argv()[0], self.get_argv().as_ptr()); libc::execvp(self.get_argv()[0], self.get_argv().as_ptr());
io::Error::last_os_error() Err(io::Error::last_os_error())
} }
#[cfg(not(any(target_os = "macos", target_os = "freebsd", #[cfg(not(any(target_os = "macos", target_os = "freebsd",