summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/parse.rs30
-rw-r--r--src/pipeline/builtins/command.rs35
-rw-r--r--src/pipeline/command.rs23
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<std::os::unix::io::RawFd> {
+ 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)?;