From 6bfb525c90ea2fe166c812e63fa20b71b0ec113d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 5 Jan 2022 17:27:07 -0500 Subject: simplify --- src/parse.rs | 30 +++++++++++++++++++++++++++++- src/pipeline/builtins/command.rs | 35 +++++------------------------------ src/pipeline/command.rs | 23 +---------------------- 3 files changed, 35 insertions(+), 53 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index 9abfaac..4baa951 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -10,7 +10,7 @@ pub enum RedirectTarget { File(std::path::PathBuf), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum Direction { In, Out, @@ -24,6 +24,34 @@ impl Direction { _ => return None, }) } + + pub fn open( + self, + path: &std::path::Path, + ) -> nix::Result { + use nix::fcntl::OFlag; + use nix::sys::stat::Mode; + Ok(match self { + crate::parse::Direction::In => nix::fcntl::open( + path, + OFlag::O_NOCTTY | OFlag::O_RDONLY, + Mode::empty(), + )?, + crate::parse::Direction::Out => nix::fcntl::open( + path, + OFlag::O_CREAT + | OFlag::O_NOCTTY + | OFlag::O_WRONLY + | OFlag::O_TRUNC, + Mode::S_IRUSR + | Mode::S_IWUSR + | Mode::S_IRGRP + | Mode::S_IWGRP + | Mode::S_IROTH + | Mode::S_IWOTH, + )?, + }) + } } #[derive(Debug, Clone)] diff --git a/src/pipeline/builtins/command.rs b/src/pipeline/builtins/command.rs index d65dd1c..c0fa86d 100644 --- a/src/pipeline/builtins/command.rs +++ b/src/pipeline/builtins/command.rs @@ -137,38 +137,13 @@ impl Io { pub fn apply_redirects(&mut self, redirects: &[crate::parse::Redirect]) { for redirect in redirects { - match &redirect.to { - crate::parse::RedirectTarget::Fd(fd) => { - self.fds.insert(redirect.from, self.fds[fd]); - } + let to = match &redirect.to { + crate::parse::RedirectTarget::Fd(fd) => self.fds[fd], crate::parse::RedirectTarget::File(path) => { - use nix::fcntl::OFlag; - use nix::sys::stat::Mode; - let fd = match redirect.dir { - crate::parse::Direction::In => nix::fcntl::open( - path, - OFlag::O_NOCTTY | OFlag::O_RDONLY, - Mode::empty(), - ) - .unwrap(), - crate::parse::Direction::Out => nix::fcntl::open( - path, - OFlag::O_CREAT - | OFlag::O_NOCTTY - | OFlag::O_WRONLY - | OFlag::O_TRUNC, - Mode::S_IRUSR - | Mode::S_IWUSR - | Mode::S_IRGRP - | Mode::S_IWGRP - | Mode::S_IROTH - | Mode::S_IWOTH, - ) - .unwrap(), - }; - self.fds.insert(redirect.from, fd); + redirect.dir.open(path).unwrap() } - } + }; + self.fds.insert(redirect.from, to); } } diff --git a/src/pipeline/command.rs b/src/pipeline/command.rs index dceb732..746a340 100644 --- a/src/pipeline/command.rs +++ b/src/pipeline/command.rs @@ -189,28 +189,7 @@ fn apply_redirects( nix::unistd::dup2(*fd, redirect.from)?; } crate::parse::RedirectTarget::File(path) => { - use nix::fcntl::OFlag; - use nix::sys::stat::Mode; - let fd = match redirect.dir { - crate::parse::Direction::In => nix::fcntl::open( - path, - OFlag::O_NOCTTY | OFlag::O_RDONLY, - Mode::empty(), - )?, - crate::parse::Direction::Out => nix::fcntl::open( - path, - OFlag::O_CREAT - | OFlag::O_NOCTTY - | OFlag::O_WRONLY - | OFlag::O_TRUNC, - Mode::S_IRUSR - | Mode::S_IWUSR - | Mode::S_IRGRP - | Mode::S_IWGRP - | Mode::S_IROTH - | Mode::S_IWOTH, - )?, - }; + let fd = redirect.dir.open(path)?; if fd != redirect.from { nix::unistd::dup2(fd, redirect.from)?; nix::unistd::close(fd)?; -- cgit v1.2.3-54-g00ecf