IoFile: Wrap File instead of OwnedFd

This commit is contained in:
Mahmoud Al-Qudsi 2024-03-23 00:44:27 -05:00
parent 6f9f9ee400
commit 99c9d6eef6

View File

@ -21,6 +21,7 @@ use libc::{EAGAIN, EINTR, ENOENT, ENOTDIR, EPIPE, EWOULDBLOCK, STDOUT_FILENO};
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 @@ impl IoData for IoFd {
/// 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 @@ impl IoData for IoFile {
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 @@ impl IoChain {
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 {