2023-02-04 18:21:42 +08:00
|
|
|
//! This file supports specifying and applying redirections.
|
|
|
|
|
2023-10-09 05:22:27 +08:00
|
|
|
use crate::io::IoChain;
|
2023-08-09 06:16:04 +08:00
|
|
|
use crate::wchar::prelude::*;
|
2023-02-04 18:21:42 +08:00
|
|
|
use crate::wutil::fish_wcstoi;
|
2024-01-21 07:21:49 +08:00
|
|
|
use nix::fcntl::OFlag;
|
2023-02-04 18:21:42 +08:00
|
|
|
use std::os::fd::RawFd;
|
|
|
|
|
2024-01-02 04:29:05 +08:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum RedirectionMode {
|
|
|
|
overwrite, // normal redirection: > file.txt
|
|
|
|
append, // appending redirection: >> file.txt
|
|
|
|
input, // input redirection: < file.txt
|
|
|
|
fd, // fd redirection: 2>&1
|
|
|
|
noclob, // noclobber redirection: >? file.txt
|
|
|
|
}
|
2023-02-04 18:21:42 +08:00
|
|
|
|
2024-01-02 04:29:05 +08:00
|
|
|
/// A type that represents the action dup2(src, target).
|
|
|
|
/// If target is negative, this represents close(src).
|
|
|
|
/// Note none of the fds here are considered 'owned'.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Dup2Action {
|
|
|
|
pub src: i32,
|
|
|
|
pub target: i32,
|
2023-02-04 18:21:42 +08:00
|
|
|
}
|
|
|
|
|
2024-01-02 04:29:05 +08:00
|
|
|
/// A class representing a sequence of basic redirections.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Dup2List {
|
|
|
|
/// The list of actions.
|
|
|
|
pub actions: Vec<Dup2Action>,
|
|
|
|
}
|
2023-02-04 18:21:42 +08:00
|
|
|
|
|
|
|
impl RedirectionMode {
|
|
|
|
/// The open flags for this redirection mode.
|
2024-01-21 07:21:49 +08:00
|
|
|
pub fn oflags(self) -> Option<OFlag> {
|
2023-02-04 18:21:42 +08:00
|
|
|
match self {
|
2024-01-21 07:21:49 +08:00
|
|
|
RedirectionMode::append => Some(OFlag::O_CREAT | OFlag::O_APPEND | OFlag::O_WRONLY),
|
|
|
|
RedirectionMode::overwrite => Some(OFlag::O_CREAT | OFlag::O_WRONLY | OFlag::O_TRUNC),
|
|
|
|
RedirectionMode::noclob => Some(OFlag::O_CREAT | OFlag::O_EXCL | OFlag::O_WRONLY),
|
|
|
|
RedirectionMode::input => Some(OFlag::O_RDONLY),
|
2023-02-04 18:21:42 +08:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 05:22:27 +08:00
|
|
|
impl Dup2Action {
|
|
|
|
pub fn new(src: RawFd, target: RawFd) -> Self {
|
|
|
|
Self { src, target }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 18:21:42 +08:00
|
|
|
/// A struct which represents a redirection specification from the user.
|
|
|
|
/// Here the file descriptors don't represent open files - it's purely textual.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RedirectionSpec {
|
|
|
|
/// The redirected fd, or -1 on overflow.
|
|
|
|
/// In the common case of a pipe, this is 1 (STDOUT_FILENO).
|
|
|
|
/// For example, in the case of "3>&1" this will be 3.
|
2023-04-09 19:37:36 +08:00
|
|
|
pub fd: RawFd,
|
2023-02-04 18:21:42 +08:00
|
|
|
|
|
|
|
/// The redirection mode.
|
2023-04-09 19:37:36 +08:00
|
|
|
pub mode: RedirectionMode,
|
2023-02-04 18:21:42 +08:00
|
|
|
|
|
|
|
/// The target of the redirection.
|
|
|
|
/// For example in "3>&1", this will be "1".
|
|
|
|
/// In "< file.txt" this will be "file.txt".
|
2023-04-09 19:37:36 +08:00
|
|
|
pub target: WString,
|
2023-02-04 18:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedirectionSpec {
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn new(fd: RawFd, mode: RedirectionMode, target: WString) -> Self {
|
|
|
|
Self { fd, mode, target }
|
|
|
|
}
|
2023-02-04 18:21:42 +08:00
|
|
|
/// \return if this is a close-type redirection.
|
|
|
|
pub fn is_close(&self) -> bool {
|
2024-01-13 15:25:12 +08:00
|
|
|
self.mode == RedirectionMode::fd && self.target == "-"
|
2023-02-04 18:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to parse target as an fd.
|
|
|
|
pub fn get_target_as_fd(&self) -> Option<RawFd> {
|
2023-03-06 11:52:12 +08:00
|
|
|
fish_wcstoi(&self.target).ok()
|
2023-02-04 18:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \return the open flags for this redirection.
|
2024-01-21 07:21:49 +08:00
|
|
|
pub fn oflags(&self) -> OFlag {
|
2023-02-04 18:21:42 +08:00
|
|
|
match self.mode.oflags() {
|
|
|
|
Some(flags) => flags,
|
|
|
|
None => panic!("Not a file redirection"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 19:38:18 +08:00
|
|
|
pub type RedirectionSpecList = Vec<RedirectionSpec>;
|
2023-02-04 18:21:42 +08:00
|
|
|
|
|
|
|
/// Produce a dup_fd_list_t from an io_chain. This may not be called before fork().
|
|
|
|
/// The result contains the list of fd actions (dup2 and close), as well as the list
|
|
|
|
/// of fds opened.
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn dup2_list_resolve_chain(io_chain: &IoChain) -> Dup2List {
|
|
|
|
let mut result = Dup2List { actions: vec![] };
|
|
|
|
for io in &io_chain.0 {
|
|
|
|
if io.source_fd() < 0 {
|
|
|
|
result.add_close(io.fd())
|
|
|
|
} else {
|
|
|
|
result.add_dup2(io.source_fd(), io.fd())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-02-04 18:21:42 +08:00
|
|
|
impl Dup2List {
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
2023-02-04 18:21:42 +08:00
|
|
|
/// \return the list of dup2 actions.
|
2023-08-13 02:35:16 +08:00
|
|
|
pub fn get_actions(&self) -> &[Dup2Action] {
|
2023-02-04 18:21:42 +08:00
|
|
|
&self.actions
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \return the fd ultimately dup'd to a target fd, or -1 if the target is closed.
|
|
|
|
/// For example, if target fd is 1, and we have a dup2 chain 5->3 and 3->1, then we will
|
|
|
|
/// return 5. If the target is not referenced in the chain, returns target.
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn fd_for_target_fd(&self, target: RawFd) -> RawFd {
|
2023-02-04 18:21:42 +08:00
|
|
|
// Paranoia.
|
|
|
|
if target < 0 {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
// Note we can simply walk our action list backwards, looking for src -> target dups.
|
|
|
|
let mut cursor = target;
|
|
|
|
for action in self.actions.iter().rev() {
|
|
|
|
if action.target == cursor {
|
|
|
|
// cursor is replaced by action.src
|
|
|
|
cursor = action.src;
|
|
|
|
} else if action.src == cursor && action.target < 0 {
|
|
|
|
// cursor is closed.
|
|
|
|
cursor = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Append a dup2 action.
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn add_dup2(&mut self, src: RawFd, target: RawFd) {
|
2023-02-04 18:21:42 +08:00
|
|
|
assert!(src >= 0 && target >= 0, "Invalid fd in add_dup2");
|
|
|
|
// Note: record these even if src and target is the same.
|
|
|
|
// This is a note that we must clear the CLO_EXEC bit.
|
|
|
|
self.actions.push(Dup2Action { src, target });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Append a close action.
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn add_close(&mut self, fd: RawFd) {
|
2023-02-04 18:21:42 +08:00
|
|
|
assert!(fd >= 0, "Invalid fd in add_close");
|
|
|
|
self.actions.push(Dup2Action {
|
|
|
|
src: fd,
|
|
|
|
target: -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|