From 99c9d6eef6da6af2b0062ed70409f4aec7ec3efa Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sat, 23 Mar 2024 00:44:27 -0500 Subject: [PATCH] IoFile: Wrap File instead of OwnedFd --- src/io.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/io.rs b/src/io.rs index eb00e3583..1c9284d6f 100644 --- a/src/io.rs +++ b/src/io.rs @@ -21,6 +21,7 @@ use nix::fcntl::OFlag; use nix::sys::stat::Mode; use std::cell::{RefCell, UnsafeCell}; +use std::fs::File; use std::io; use std::os::fd::{AsRawFd, IntoRawFd, OwnedFd, RawFd}; use std::sync::atomic::{AtomicU64, Ordering}; @@ -264,12 +265,12 @@ fn as_ptr(&self) -> *const () { /// Represents a redirection to or from an opened file. pub struct IoFile { fd: RawFd, - // The fd for the file which we are writing to or reading from. - file_fd: OwnedFd, + // The file which we are writing to or reading from. + file: File, } impl IoFile { - pub fn new(fd: RawFd, file_fd: OwnedFd) -> Self { - IoFile { fd, file_fd } + pub fn new(fd: RawFd, file: File) -> Self { + IoFile { fd, file } // Invalid file redirections are replaced with a closed fd, so the following // assertion isn't guaranteed to pass: // assert(file_fd_.valid() && "File is not valid"); @@ -283,10 +284,10 @@ fn fd(&self) -> RawFd { self.fd } fn source_fd(&self) -> RawFd { - self.file_fd.as_raw_fd() + self.file.as_raw_fd() } fn print(&self) { - eprintf!("file %d -> %d\n", self.file_fd.as_raw_fd(), self.fd) + eprintf!("file %d -> %d\n", self.file.as_raw_fd(), self.fd) } fn as_ptr(&self) -> *const () { (self as *const Self).cast() @@ -666,7 +667,8 @@ pub fn append_from_specs(&mut self, specs: &RedirectionSpecList, pwd: &wstr) -> match wopen_cloexec(&path, oflags, OPEN_MASK) { Ok(fd) => { - self.push(Arc::new(IoFile::new(spec.fd, fd))); + let file = File::from(fd); + self.push(Arc::new(IoFile::new(spec.fd, file))); } Err(err) => { if oflags.contains(OFlag::O_EXCL) && err == nix::Error::EEXIST {