-
-
Notifications
You must be signed in to change notification settings - Fork 748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filedescriptor crate to support dup2 #786
Comments
There's no direct support for This is the code used for There are two flavors: one that atomically dup's with CLOEXEC set, and one that non-atomically does the same. I'd suggest making the method have an unsafe signature to represent that there is potential danger in re-assigning the fd: #[cfg(unix)]
unsafe pub fn dup2<F: AsRawFileDescriptor>(f: &F, dest_fd: RawFd) -> Result<Self>; If you wanted to portably redirect stdio streams on windows you can't literally use If you also wanted to add a portable API for that to filedescriptor I'm open to it; I think you'd need to add an enum representing stdin, stdout and stderr and then have it call the appropriate |
I've opened up a premature PR with only windows directs supported for now: #788 |
I'd suggest something like: #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum StdioDescriptor {
Stdin,
Stdout,
Stderr,
}
impl FileDescriptor {
/// Duplicates the descriptor representing `stdio`, then re-assigns that descriptor
/// by duplicating `f`.
/// Returns the original decriptor.
pub fn redirect_stdio<F: AsRawFileDescriptor>(f: &F, stdio: StdioDescriptor) -> Result<Self>;
} Under the covers that would use I'd prefer not to include the restore-on-drop functionality in this crate; struct TemporaryRedirection {
stdio: StdioDescriptor,
fd: FileDescriptor
}
impl TemporaryRedirection {
pub fn new<F: AsRawFileDescriptor>(f: &F, stdio: StdioDescriptor) -> Result<Self> {
let fd = FileDescriptor::redirect_stdio(f, stdio)?;
Ok(Self { fd, stdio }}
}
}
impl Drop for TemporaryRedirection {
fn drop(&mut self) {
let _ = FileDescriptor::redirect_stdio(self.fd, self.stdio);
}
} |
Sounds good - will just return the original file descriptor in the redirect_stdio method like you mentioned above then 👍 |
@wez have also made a PR for the gag crate now: Stebalien/gag-rs#7 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Hi Wez. Am currently looking into tweaking the gag crate so that it works cross platform and wanted to use your filedescriptor crate to do it. Was wandering whether your crate supports dup2? Need this feature in order to redirect stdout/stderr into file. Happy to submit a PR for this, although I don't do much low level stuff so might need a couple pointers here..
The text was updated successfully, but these errors were encountered: