Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
doy committed Jan 5, 2022
1 parent c4b5c55 commit 6bfb525
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
30 changes: 29 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum RedirectTarget {
File(std::path::PathBuf),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum Direction {
In,
Out,
Expand All @@ -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)]
Expand Down
35 changes: 5 additions & 30 deletions src/pipeline/builtins/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
23 changes: 1 addition & 22 deletions src/pipeline/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit 6bfb525

Please sign in to comment.