mirror of
https://github.com/openharmony/third_party_rust_rustix.git
synced 2026-07-19 23:03:36 -04:00
Remove rustix::io::OwnedFd.
Remove rustix::io::OwnedFd, and just use the std and io_lifetimes' OwnedFd definition. Now that rustix is using io_lifetimes 1.0, which has switched to using the `OwnedFd` in std, this is no longer needed. This simplifies rustix' API, as users no longer have to be aware of the difference between rustix::io::OwnedFd and rustix::fd::OwnedFd.
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use super::c;
|
||||
use super::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, LibcFd, RawFd};
|
||||
use super::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, LibcFd, OwnedFd, RawFd};
|
||||
#[cfg(not(windows))]
|
||||
use super::offset::libc_off_t;
|
||||
#[cfg(not(windows))]
|
||||
use crate::ffi::CStr;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
#[cfg(windows)]
|
||||
use core::convert::TryInto;
|
||||
|
||||
|
||||
@@ -46,9 +46,9 @@ use super::super::offset::{libc_fstatvfs, libc_statvfs};
|
||||
target_env = "gnu",
|
||||
))]
|
||||
use super::super::time::types::LibcTimespec;
|
||||
use crate::fd::BorrowedFd;
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
use crate::fd::RawFd;
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::ffi::CStr;
|
||||
#[cfg(any(target_os = "ios", target_os = "macos"))]
|
||||
use crate::ffi::CString;
|
||||
@@ -102,7 +102,7 @@ use crate::fs::{Dev, FileType};
|
||||
use crate::fs::{FdFlags, Mode, OFlags, Stat, Timestamps};
|
||||
#[cfg(not(any(target_os = "illumos", target_os = "redox", target_os = "wasi")))]
|
||||
use crate::fs::{StatVfs, StatVfsMountFlags};
|
||||
use crate::io::{self, OwnedFd, SeekFrom};
|
||||
use crate::io::{self, SeekFrom};
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
use crate::process::{Gid, Uid};
|
||||
#[cfg(not(all(
|
||||
|
||||
@@ -59,10 +59,10 @@
|
||||
|
||||
use super::super::c;
|
||||
use super::super::conv::{ret, ret_owned_fd, ret_u32};
|
||||
use crate::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
||||
use crate::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
|
||||
#[cfg(not(feature = "rustc-dep-of-std"))]
|
||||
use crate::fd::{FromRawFd, IntoRawFd};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use alloc::vec::Vec;
|
||||
use bitflags::bitflags;
|
||||
use core::convert::TryInto;
|
||||
|
||||
@@ -11,12 +11,12 @@ use super::super::offset::{libc_pread, libc_pwrite};
|
||||
use super::super::offset::{libc_preadv, libc_pwritev};
|
||||
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
||||
use super::super::offset::{libc_preadv2, libc_pwritev2};
|
||||
use crate::fd::{AsFd, BorrowedFd, RawFd};
|
||||
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
use crate::io::DupFlags;
|
||||
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "wasi")))]
|
||||
use crate::io::PipeFlags;
|
||||
use crate::io::{self, IoSlice, IoSliceMut, OwnedFd, PollFd};
|
||||
use crate::io::{self, IoSlice, IoSliceMut, PollFd};
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
use crate::io::{EventfdFlags, ReadWriteFlags};
|
||||
use core::cmp::min;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
use super::super::c;
|
||||
use super::super::conv::{borrowed_fd, syscall_ret, syscall_ret_owned_fd, syscall_ret_u32};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::io_uring::{io_uring_params, IoringEnterFlags, IoringRegisterOp};
|
||||
use linux_raw_sys::general::{__NR_io_uring_enter, __NR_io_uring_register, __NR_io_uring_setup};
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ use super::types::{MapFlags, MprotectFlags, MsyncFlags, ProtFlags};
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
use super::types::{MlockFlags, UserfaultfdFlags};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io;
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
use crate::io::OwnedFd;
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
|
||||
#[cfg(not(target_os = "redox"))]
|
||||
pub(crate) fn madvise(addr: *mut c::c_void, len: usize, advice: Advice) -> io::Result<()> {
|
||||
|
||||
@@ -15,8 +15,8 @@ use super::send_recv::{RecvFlags, SendFlags};
|
||||
use super::types::{AcceptFlags, AddressFamily, Protocol, Shutdown, SocketFlags, SocketType};
|
||||
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
|
||||
use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
|
||||
use crate::utils::as_ptr;
|
||||
use core::convert::TryInto;
|
||||
|
||||
@@ -15,8 +15,7 @@ use core::mem::MaybeUninit;
|
||||
#[cfg(feature = "time")]
|
||||
use {
|
||||
super::super::conv::{borrowed_fd, ret_owned_fd},
|
||||
crate::fd::BorrowedFd,
|
||||
crate::io::OwnedFd,
|
||||
crate::fd::{BorrowedFd, OwnedFd},
|
||||
crate::time::{Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags},
|
||||
};
|
||||
|
||||
|
||||
@@ -30,9 +30,10 @@ use super::reg::{raw_arg, ArgNumber, ArgReg, RetReg, R0};
|
||||
use super::time::types::ClockId;
|
||||
#[cfg(feature = "time")]
|
||||
use super::time::types::TimerfdClockId;
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::ffi::CStr;
|
||||
use crate::fs::{FileType, Mode, OFlags};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::process::{Pid, Resource, Signal};
|
||||
use crate::utils::{as_mut_ptr, as_ptr};
|
||||
use core::mem::MaybeUninit;
|
||||
@@ -746,9 +747,7 @@ pub(super) unsafe fn ret_usize_infallible(raw: RetReg<R0>) -> usize {
|
||||
#[inline]
|
||||
pub(super) unsafe fn ret_owned_fd(raw: RetReg<R0>) -> io::Result<OwnedFd> {
|
||||
let raw_fd = try_decode_raw_fd(raw)?;
|
||||
Ok(OwnedFd::from(crate::backend::fd::OwnedFd::from_raw_fd(
|
||||
raw_fd,
|
||||
)))
|
||||
Ok(crate::backend::fd::OwnedFd::from_raw_fd(raw_fd))
|
||||
}
|
||||
|
||||
/// Convert the return value of `dup2` and `dup3`.
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::fd::{AsFd, BorrowedFd};
|
||||
use crate::fd::{AsFd, BorrowedFd, OwnedFd};
|
||||
use crate::ffi::{CStr, CString};
|
||||
use crate::fs::{
|
||||
fcntl_getfl, fstat, fstatfs, fstatvfs, openat, FileType, Mode, OFlags, Stat, StatFs, StatVfs,
|
||||
};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::process::fchdir;
|
||||
use crate::utils::as_ptr;
|
||||
use alloc::borrow::ToOwned;
|
||||
|
||||
@@ -21,14 +21,14 @@ use super::super::conv::{loff_t, loff_t_from_u64, ret_u64};
|
||||
target_pointer_width = "32",
|
||||
))]
|
||||
use crate::fd::AsFd;
|
||||
use crate::fd::{BorrowedFd, RawFd};
|
||||
use crate::fd::{BorrowedFd, OwnedFd, RawFd};
|
||||
use crate::ffi::CStr;
|
||||
use crate::fs::{
|
||||
Access, Advice, AtFlags, FallocateFlags, FdFlags, FileType, FlockOperation, MemfdFlags, Mode,
|
||||
OFlags, RenameFlags, ResolveFlags, SealFlags, Stat, StatFs, StatVfs, StatVfsMountFlags,
|
||||
StatxFlags, Timestamps,
|
||||
};
|
||||
use crate::io::{self, OwnedFd, SeekFrom};
|
||||
use crate::io::{self, SeekFrom};
|
||||
use crate::process::{Gid, Uid};
|
||||
use core::convert::TryInto;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
@@ -61,10 +61,10 @@
|
||||
|
||||
use super::super::c;
|
||||
use crate::backend::io::syscalls::{epoll_add, epoll_create, epoll_del, epoll_mod, epoll_wait};
|
||||
use crate::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
||||
use crate::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
|
||||
#[cfg(feature = "std")]
|
||||
use crate::fd::{FromRawFd, IntoRawFd};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use alloc::vec::Vec;
|
||||
use bitflags::bitflags;
|
||||
use core::fmt;
|
||||
@@ -261,7 +261,7 @@ impl<'context, T: AsFd + Into<OwnedFd> + From<OwnedFd>> Context for Owning<'cont
|
||||
// Safety: The file descriptor was held by the kernel epoll object and
|
||||
// is now being released, so we can create a new `OwnedFd` that assumes
|
||||
// ownership.
|
||||
unsafe { T::from(io_lifetimes::OwnedFd::from_raw_fd(raw_fd).into()) }
|
||||
unsafe { T::from(io_lifetimes::OwnedFd::from_raw_fd(raw_fd)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,9 @@ use super::super::conv::{
|
||||
};
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
use super::super::conv::{hi, lo};
|
||||
use crate::fd::{AsFd, BorrowedFd, RawFd};
|
||||
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
|
||||
use crate::io::{
|
||||
self, epoll, DupFlags, EventfdFlags, IoSlice, IoSliceMut, OwnedFd, PipeFlags, PollFd,
|
||||
ReadWriteFlags,
|
||||
self, epoll, DupFlags, EventfdFlags, IoSlice, IoSliceMut, PipeFlags, PollFd, ReadWriteFlags,
|
||||
};
|
||||
#[cfg(feature = "net")]
|
||||
use crate::net::{RecvFlags, SendFlags};
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
#![allow(clippy::undocumented_unsafe_blocks)]
|
||||
|
||||
use super::super::conv::{by_mut, c_uint, pass_usize, ret, ret_c_uint, ret_owned_fd};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::io::OwnedFd;
|
||||
use crate::io_uring::{io_uring_params, IoringEnterFlags, IoringRegisterOp};
|
||||
use core::ffi::c_void;
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ use super::types::{
|
||||
Advice, MapFlags, MlockFlags, MprotectFlags, MremapFlags, MsyncFlags, ProtFlags,
|
||||
UserfaultfdFlags,
|
||||
};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::io;
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
use core::convert::TryInto;
|
||||
use linux_raw_sys::general::MAP_ANONYMOUS;
|
||||
|
||||
@@ -15,8 +15,8 @@ use super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os,
|
||||
use super::send_recv::{RecvFlags, SendFlags};
|
||||
use super::types::{AcceptFlags, AddressFamily, Protocol, Shutdown, SocketFlags, SocketType};
|
||||
use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{BorrowedFd, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::net::{SocketAddrAny, SocketAddrUnix, SocketAddrV4, SocketAddrV6};
|
||||
use c::{sockaddr, sockaddr_in, sockaddr_in6, socklen_t};
|
||||
use core::convert::TryInto;
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
|
||||
use super::super::c;
|
||||
use super::super::elf::*;
|
||||
use crate::fd::OwnedFd;
|
||||
#[cfg(feature = "param")]
|
||||
use crate::ffi::CStr;
|
||||
use crate::fs::{Mode, OFlags};
|
||||
use crate::io::OwnedFd;
|
||||
use crate::utils::as_ptr;
|
||||
use crate::utils::check_raw_pointer;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
@@ -12,9 +12,9 @@ use super::super::conv::{ret, ret_infallible};
|
||||
use super::types::ClockId;
|
||||
#[cfg(feature = "time")]
|
||||
use crate::fd::BorrowedFd;
|
||||
use crate::io;
|
||||
#[cfg(feature = "time")]
|
||||
use crate::io::OwnedFd;
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
#[cfg(feature = "time")]
|
||||
use crate::time::{Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags};
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@
|
||||
//!
|
||||
//! [`cwd`]: crate::fs::cwd
|
||||
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::ffi::{CStr, CString};
|
||||
#[cfg(not(target_os = "illumos"))]
|
||||
use crate::fs::Access;
|
||||
@@ -15,7 +16,7 @@ use crate::fs::FileType;
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
use crate::fs::RenameFlags;
|
||||
use crate::fs::{AtFlags, Mode, OFlags, Stat, Timestamps};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::path::SMALL_PATH_BUFFER_SIZE;
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
use crate::process::{Gid, Uid};
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@
|
||||
//! can have dedicated static type signatures.
|
||||
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use backend::fd::{AsFd, RawFd};
|
||||
use crate::io;
|
||||
use backend::fd::{AsFd, OwnedFd, RawFd};
|
||||
use backend::fs::types::{FdFlags, OFlags};
|
||||
|
||||
/// `fcntl(fd, F_GETFD)`—Returns a file descriptor's flags.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
use crate::{backend, path};
|
||||
|
||||
pub use backend::fs::types::MemfdFlags;
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
use crate::{backend, path};
|
||||
use backend::fd::AsFd;
|
||||
use backend::fs::types::{Mode, OFlags, ResolveFlags};
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
//! Functions which duplicate file descriptors.
|
||||
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
use backend::fd::AsFd;
|
||||
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
|
||||
pub use backend::io::types::EventfdFlags;
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ mod ioctl;
|
||||
#[cfg(not(any(windows, target_os = "redox")))]
|
||||
#[cfg(feature = "net")]
|
||||
mod is_read_write;
|
||||
mod owned_fd;
|
||||
#[cfg(not(any(windows, target_os = "wasi")))]
|
||||
mod pipe;
|
||||
mod poll;
|
||||
@@ -45,7 +44,6 @@ pub use ioctl::{ioctl_tiocexcl, ioctl_tiocnxcl};
|
||||
#[cfg(not(any(windows, target_os = "redox")))]
|
||||
#[cfg(feature = "net")]
|
||||
pub use is_read_write::is_read_write;
|
||||
pub use owned_fd::OwnedFd;
|
||||
#[cfg(not(any(windows, target_os = "wasi")))]
|
||||
pub use pipe::pipe;
|
||||
#[cfg(not(any(
|
||||
|
||||
@@ -1,366 +0,0 @@
|
||||
//! A wrapper around [`io_lifetimes::OwnedFd`].
|
||||
//!
|
||||
//! rustix needs to wrap io-lifetimes' `OwnedFd` type so that it can call its
|
||||
//! own [`close`] function when the `OwnedFd` is dropped.
|
||||
//!
|
||||
//! [`close`]: crate::io::close
|
||||
//!
|
||||
//! # Safety
|
||||
//!
|
||||
//! We wrap an `OwnedFd` in a `ManuallyDrop` so that we can extract the
|
||||
//! file descriptor and close it ourselves.
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use crate::backend::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use crate::io::close;
|
||||
use core::fmt;
|
||||
use core::mem::{forget, ManuallyDrop};
|
||||
|
||||
/// A wrapper around [`io_lifetimes::OwnedFd`] which closes the file descriptor
|
||||
/// using rustix's own [`close`] rather than libc's `close`.
|
||||
///
|
||||
/// [`close`]: crate::io::close
|
||||
#[repr(transparent)]
|
||||
pub struct OwnedFd {
|
||||
inner: ManuallyDrop<crate::backend::fd::OwnedFd>,
|
||||
}
|
||||
|
||||
impl OwnedFd {
|
||||
/// Creates a new `OwnedFd` instance that shares the same underlying file
|
||||
/// handle as the existing `OwnedFd` instance.
|
||||
#[cfg(all(unix, not(target_os = "wasi")))]
|
||||
pub fn try_clone(&self) -> crate::io::Result<Self> {
|
||||
// We want to atomically duplicate this file descriptor and set the
|
||||
// CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
|
||||
// is a POSIX flag that was added to Linux in 2.6.24.
|
||||
#[cfg(not(target_os = "espidf"))]
|
||||
let fd = crate::fs::fcntl_dupfd_cloexec(self, 0)?;
|
||||
|
||||
// For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics
|
||||
// will never be supported, as this is a bare metal framework with
|
||||
// no capabilities for multi-process execution. While F_DUPFD is also
|
||||
// not supported yet, it might be (currently it returns ENOSYS).
|
||||
#[cfg(target_os = "espidf")]
|
||||
let fd = crate::fs::fcntl_dupfd(self)?;
|
||||
|
||||
Ok(fd)
|
||||
}
|
||||
|
||||
/// Creates a new `OwnedFd` instance that shares the same underlying file
|
||||
/// handle as the existing `OwnedFd` instance.
|
||||
#[cfg(target_os = "wasi")]
|
||||
pub fn try_clone(&self) -> std::io::Result<Self> {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Unsupported,
|
||||
"operation not supported on WASI yet",
|
||||
))
|
||||
}
|
||||
|
||||
/// Creates a new `OwnedFd` instance that shares the same underlying file
|
||||
/// handle as the existing `OwnedFd` instance.
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn try_clone(&self) -> std::io::Result<Self> {
|
||||
use windows_sys::Win32::Networking::WinSock::{
|
||||
WSADuplicateSocketW, WSAGetLastError, WSASocketW, INVALID_SOCKET, SOCKET_ERROR,
|
||||
WSAEINVAL, WSAEPROTOTYPE, WSAPROTOCOL_INFOW, WSA_FLAG_NO_HANDLE_INHERIT,
|
||||
WSA_FLAG_OVERLAPPED,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcessId;
|
||||
|
||||
let mut info = unsafe { std::mem::zeroed::<WSAPROTOCOL_INFOW>() };
|
||||
let result =
|
||||
unsafe { WSADuplicateSocketW(self.as_raw_fd() as _, GetCurrentProcessId(), &mut info) };
|
||||
match result {
|
||||
SOCKET_ERROR => return Err(std::io::Error::last_os_error()),
|
||||
0 => (),
|
||||
_ => panic!(),
|
||||
}
|
||||
let socket = unsafe {
|
||||
WSASocketW(
|
||||
info.iAddressFamily,
|
||||
info.iSocketType,
|
||||
info.iProtocol,
|
||||
&mut info,
|
||||
0,
|
||||
WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT,
|
||||
)
|
||||
};
|
||||
|
||||
if socket != INVALID_SOCKET {
|
||||
unsafe { Ok(Self::from_raw_fd(socket as _)) }
|
||||
} else {
|
||||
let error = unsafe { WSAGetLastError() };
|
||||
|
||||
if error != WSAEPROTOTYPE && error != WSAEINVAL {
|
||||
return Err(std::io::Error::from_raw_os_error(error));
|
||||
}
|
||||
|
||||
let socket = unsafe {
|
||||
WSASocketW(
|
||||
info.iAddressFamily,
|
||||
info.iSocketType,
|
||||
info.iProtocol,
|
||||
&mut info,
|
||||
0,
|
||||
WSA_FLAG_OVERLAPPED,
|
||||
)
|
||||
};
|
||||
|
||||
if socket == INVALID_SOCKET {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let socket = Self::from_raw_fd(socket as _);
|
||||
socket.set_no_inherit()?;
|
||||
Ok(socket)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg(not(target_vendor = "uwp"))]
|
||||
fn set_no_inherit(&self) -> std::io::Result<()> {
|
||||
use windows_sys::Win32::Foundation::{SetHandleInformation, HANDLE, HANDLE_FLAG_INHERIT};
|
||||
match unsafe { SetHandleInformation(self.as_raw_fd() as HANDLE, HANDLE_FLAG_INHERIT, 0) } {
|
||||
0 => return Err(std::io::Error::last_os_error()),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg(target_vendor = "uwp")]
|
||||
fn set_no_inherit(&self) -> std::io::Result<()> {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Unsupported,
|
||||
"Unavailable on UWP",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
impl AsFd for OwnedFd {
|
||||
#[inline]
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.inner.as_fd()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl io_lifetimes::AsSocket for OwnedFd {
|
||||
#[inline]
|
||||
fn as_socket(&self) -> BorrowedFd<'_> {
|
||||
self.inner.as_socket()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(io_lifetimes_use_std, not(feature = "std")))]
|
||||
impl From<OwnedFd> for crate::backend::fd::OwnedFd {
|
||||
#[inline]
|
||||
fn from(owned_fd: OwnedFd) -> Self {
|
||||
let raw_fd = owned_fd.inner.as_fd().as_raw_fd();
|
||||
forget(owned_fd);
|
||||
|
||||
// Safety: We use `as_fd().as_raw_fd()` to extract the raw file
|
||||
// descriptor from `self.inner`, and then `forget` `self` so
|
||||
// that they remain valid until the new `OwnedFd` acquires them.
|
||||
unsafe { crate::backend::fd::OwnedFd::from_raw_fd(raw_fd) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(io_lifetimes_use_std, not(feature = "std")))]
|
||||
impl From<crate::backend::fd::OwnedFd> for OwnedFd {
|
||||
#[inline]
|
||||
fn from(owned_fd: crate::backend::fd::OwnedFd) -> Self {
|
||||
Self {
|
||||
inner: ManuallyDrop::new(owned_fd),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(io_lifetimes_use_std, not(feature = "std"))))]
|
||||
impl From<crate::backend::fd::OwnedFd> for OwnedFd {
|
||||
#[inline]
|
||||
fn from(fd: crate::backend::fd::OwnedFd) -> Self {
|
||||
Self {
|
||||
inner: ManuallyDrop::new(fd),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(io_lifetimes_use_std, not(feature = "std"))))]
|
||||
impl From<OwnedFd> for crate::backend::fd::OwnedFd {
|
||||
#[inline]
|
||||
fn from(fd: OwnedFd) -> Self {
|
||||
let raw_fd = fd.inner.as_fd().as_raw_fd();
|
||||
forget(fd);
|
||||
|
||||
// Safety: We use `as_fd().as_raw_fd()` to extract the raw file
|
||||
// descriptor from `self.inner`, and then `forget` `self` so
|
||||
// that they remain valid until the new `OwnedFd` acquires them.
|
||||
unsafe { Self::from_raw_fd(raw_fd) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(unix, target_os = "wasi"))]
|
||||
impl From<OwnedFd> for std::fs::File {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(unix, target_os = "wasi"))]
|
||||
impl From<std::fs::File> for OwnedFd {
|
||||
fn from(file: std::fs::File) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = file.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::net::TcpListener {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::net::TcpListener> for OwnedFd {
|
||||
fn from(listener: std::net::TcpListener) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = listener.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::net::TcpStream {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::net::TcpStream> for OwnedFd {
|
||||
fn from(stream: std::net::TcpStream) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = stream.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::net::UdpSocket {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::net::UdpSocket> for OwnedFd {
|
||||
fn from(socket: std::net::UdpSocket) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = socket.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::os::unix::net::UnixStream {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::os::unix::net::UnixStream> for OwnedFd {
|
||||
fn from(stream: std::os::unix::net::UnixStream) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = stream.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::os::unix::net::UnixListener {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::os::unix::net::UnixListener> for OwnedFd {
|
||||
fn from(listener: std::os::unix::net::UnixListener) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = listener.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<OwnedFd> for std::os::unix::net::UnixDatagram {
|
||||
fn from(owned: OwnedFd) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = owned.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::os::unix::net::UnixDatagram> for OwnedFd {
|
||||
fn from(datagram: std::os::unix::net::UnixDatagram) -> Self {
|
||||
let owned: crate::backend::fd::OwnedFd = datagram.into();
|
||||
Self::from(owned)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.inner.as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
let raw_fd = self.inner.as_fd().as_raw_fd();
|
||||
forget(self);
|
||||
raw_fd
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawFd for OwnedFd {
|
||||
#[inline]
|
||||
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
||||
Self {
|
||||
inner: ManuallyDrop::new(crate::backend::fd::OwnedFd::from_raw_fd(raw_fd)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OwnedFd {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
// Safety: We use `as_fd().as_raw_fd()` to extract the raw file
|
||||
// descriptor from `self.inner`. `self.inner` is wrapped with
|
||||
// `ManuallyDrop` so dropping it doesn't invalid them.
|
||||
unsafe {
|
||||
close(self.as_fd().as_raw_fd());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for OwnedFd {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
|
||||
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
|
||||
pub use backend::io::types::PipeFlags;
|
||||
|
||||
+2
-2
@@ -16,13 +16,13 @@
|
||||
//! namespace. So with the checking here, they may fail, but they won't be able
|
||||
//! to succeed with bogus results.
|
||||
|
||||
use crate::fd::{AsFd, BorrowedFd};
|
||||
use crate::fd::{AsFd, BorrowedFd, OwnedFd};
|
||||
use crate::ffi::CStr;
|
||||
use crate::fs::{
|
||||
cwd, fstat, fstatfs, major, openat, renameat, Dir, FileType, Mode, OFlags, Stat,
|
||||
PROC_SUPER_MAGIC,
|
||||
};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::io;
|
||||
use crate::path::DecInt;
|
||||
use crate::process::{getgid, getpid, getuid, Gid, RawGid, RawUid, Uid};
|
||||
#[cfg(feature = "rustc-dep-of-std")]
|
||||
|
||||
+4
-10
@@ -9,7 +9,7 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use crate::backend;
|
||||
use crate::io::OwnedFd;
|
||||
use crate::fd::OwnedFd;
|
||||
use backend::fd::{BorrowedFd, FromRawFd, RawFd};
|
||||
|
||||
/// `STDIN_FILENO`—Standard input, borrowed.
|
||||
@@ -69,9 +69,7 @@ pub const unsafe fn stdin() -> BorrowedFd<'static> {
|
||||
#[doc(alias = "STDIN_FILENO")]
|
||||
#[inline]
|
||||
pub unsafe fn take_stdin() -> OwnedFd {
|
||||
OwnedFd::from(backend::fd::OwnedFd::from_raw_fd(
|
||||
backend::io::types::STDIN_FILENO as RawFd,
|
||||
))
|
||||
backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDIN_FILENO as RawFd)
|
||||
}
|
||||
|
||||
/// `STDOUT_FILENO`—Standard output, borrowed.
|
||||
@@ -132,9 +130,7 @@ pub const unsafe fn stdout() -> BorrowedFd<'static> {
|
||||
#[doc(alias = "STDOUT_FILENO")]
|
||||
#[inline]
|
||||
pub unsafe fn take_stdout() -> OwnedFd {
|
||||
OwnedFd::from(backend::fd::OwnedFd::from_raw_fd(
|
||||
backend::io::types::STDOUT_FILENO as RawFd,
|
||||
))
|
||||
backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDOUT_FILENO as RawFd)
|
||||
}
|
||||
|
||||
/// `STDERR_FILENO`—Standard error, borrowed.
|
||||
@@ -194,9 +190,7 @@ pub const unsafe fn stderr() -> BorrowedFd<'static> {
|
||||
#[doc(alias = "STDERR_FILENO")]
|
||||
#[inline]
|
||||
pub unsafe fn take_stderr() -> OwnedFd {
|
||||
OwnedFd::from(backend::fd::OwnedFd::from_raw_fd(
|
||||
backend::io::types::STDERR_FILENO as RawFd,
|
||||
))
|
||||
backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDERR_FILENO as RawFd)
|
||||
}
|
||||
|
||||
/// `STDIN_FILENO`—Standard input, raw.
|
||||
|
||||
+2
-2
@@ -20,8 +20,8 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use crate::backend;
|
||||
use crate::fd::{AsFd, BorrowedFd, RawFd};
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
|
||||
use crate::io;
|
||||
use core::ffi::c_void;
|
||||
use core::ptr::null_mut;
|
||||
use linux_raw_sys::general as sys;
|
||||
|
||||
@@ -139,11 +139,6 @@ mod backend;
|
||||
///
|
||||
/// Users can use this to avoid needing to import anything else to use the same
|
||||
/// versions of these types and traits.
|
||||
///
|
||||
/// Rustix APIs that use `OwnedFd` use [`rustix::io::OwnedFd`] instead, which
|
||||
/// allows rustix to implement `close` for them.
|
||||
///
|
||||
/// [`rustix::io::OwnedFd`]: crate::io::OwnedFd
|
||||
pub mod fd {
|
||||
use super::backend;
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
|
||||
pub use backend::mm::types::UserfaultfdFlags;
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
use crate::net::{SocketAddr, SocketAddrAny, SocketAddrV4, SocketAddrV6};
|
||||
use backend::fd::{AsFd, BorrowedFd};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::backend;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::OwnedFd;
|
||||
use crate::io;
|
||||
use crate::net::{AddressFamily, Protocol, SocketFlags, SocketType};
|
||||
|
||||
/// `socketpair(domain, type_ | accept_flags, protocol)`
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
use crate::backend;
|
||||
use crate::fd::AsFd;
|
||||
use crate::io::{self, OwnedFd};
|
||||
use crate::fd::{AsFd, OwnedFd};
|
||||
use crate::io;
|
||||
|
||||
pub use backend::time::types::{Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags};
|
||||
|
||||
|
||||
+1
-3
@@ -1,8 +1,6 @@
|
||||
use rustix::fd::AsFd;
|
||||
use rustix::fd::{AsFd, AsRawFd, OwnedFd};
|
||||
use rustix::fs::{cwd, mkdirat, openat, openat2, symlinkat, Mode, OFlags, ResolveFlags};
|
||||
use rustix::io::OwnedFd;
|
||||
use rustix::{io, path};
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
||||
/// Like `openat2`, but keep retrying until it fails or succeeds.
|
||||
fn openat2_more<Fd: AsFd, P: path::Arg>(
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
#![cfg(any(target_os = "android", target_os = "linux"))]
|
||||
|
||||
use rustix::fd::AsFd;
|
||||
use rustix::fd::{AsFd, OwnedFd};
|
||||
use rustix::io::epoll::{self, Epoll};
|
||||
use rustix::io::{ioctl_fionbio, read, write, OwnedFd};
|
||||
use rustix::io::{ioctl_fionbio, read, write};
|
||||
use rustix::net::{
|
||||
accept, bind_v4, connect_v4, getsockname, listen, socket, AddressFamily, Ipv4Addr, Protocol,
|
||||
SocketAddrAny, SocketAddrV4, SocketType,
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
#[cfg(not(target_os = "redox"))]
|
||||
#[test]
|
||||
fn test_owned() {
|
||||
use rustix::fd::AsFd;
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
|
||||
#[cfg(target_os = "wasi")]
|
||||
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
|
||||
use rustix::fd::{AsFd, AsRawFd, FromRawFd, IntoRawFd};
|
||||
|
||||
let file = rustix::fs::openat(
|
||||
rustix::fs::cwd(),
|
||||
@@ -22,7 +18,7 @@ fn test_owned() {
|
||||
let inner = file.into_raw_fd();
|
||||
assert_eq!(raw, inner);
|
||||
|
||||
let new = unsafe { rustix::io::OwnedFd::from_raw_fd(inner) };
|
||||
let new = unsafe { rustix::fd::OwnedFd::from_raw_fd(inner) };
|
||||
let mut buf = [0_u8; 4];
|
||||
let _ = rustix::io::read(&new, &mut buf).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user