From e978ca4aa2b7e81608a9588717508fe948ab0fad Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval R Date: Wed, 28 Dec 2022 10:08:49 +0100 Subject: [PATCH 1/3] Port file_open_tmp to safe-io --- glib/src/functions.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/glib/src/functions.rs b/glib/src/functions.rs index 851467740bbe..37e3dd660d2c 100644 --- a/glib/src/functions.rs +++ b/glib/src/functions.rs @@ -9,7 +9,7 @@ use std::mem; #[cfg(feature = "v2_58")] use std::os::unix::io::{AsFd, AsRawFd}; #[cfg(not(windows))] -use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{FromRawFd, OwnedFd, RawFd}; use std::ptr; // #[cfg(windows)] @@ -261,7 +261,7 @@ pub fn unix_open_pipe(flags: i32) -> Result<(RawFd, RawFd), Error> { #[doc(alias = "g_file_open_tmp")] pub fn file_open_tmp( tmpl: Option>, -) -> Result<(RawFd, std::path::PathBuf), crate::Error> { +) -> Result<(OwnedFd, std::path::PathBuf), crate::Error> { unsafe { let mut name_used = ptr::null_mut(); let mut error = ptr::null_mut(); @@ -271,7 +271,7 @@ pub fn file_open_tmp( &mut error, ); if error.is_null() { - Ok((ret.into_raw_fd(), from_glib_full(name_used))) + Ok((OwnedFd::from_raw_fd(ret), from_glib_full(name_used))) } else { Err(from_glib_full(error)) } From 80fceb19c79840dd0c59d794ef7f5c0d3727bab4 Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval R Date: Wed, 28 Dec 2022 10:14:07 +0100 Subject: [PATCH 2/3] unix_fd_list: Port to safe-io --- gio/src/unix_fd_list.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/gio/src/unix_fd_list.rs b/gio/src/unix_fd_list.rs index 220bc7f970f9..06dd8cdc0a5e 100644 --- a/gio/src/unix_fd_list.rs +++ b/gio/src/unix_fd_list.rs @@ -1,12 +1,12 @@ // Take a look at the license at the top of the repository in the LICENSE file. #[cfg(unix)] -use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use std::{mem, ptr}; use glib::{prelude::*, translate::*}; #[cfg(all(not(unix), docsrs))] -use socket::{AsRawFd, IntoRawFd, RawFd}; +use socket::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use crate::{ffi, UnixFDList}; @@ -25,12 +25,12 @@ impl UnixFDList { pub trait UnixFDListExtManual: IsA + Sized { #[doc(alias = "g_unix_fd_list_append")] - fn append(&self, fd: T) -> Result { + fn append(&self, fd: impl AsFd) -> Result { unsafe { let mut error = ptr::null_mut(); let ret = ffi::g_unix_fd_list_append( self.as_ref().to_glib_none().0, - fd.as_raw_fd(), + fd.as_fd().as_raw_fd(), &mut error, ); if error.is_null() { @@ -42,12 +42,14 @@ pub trait UnixFDListExtManual: IsA + Sized { } #[doc(alias = "g_unix_fd_list_get")] - fn get(&self, index_: i32) -> Result { + fn get(&self, index_: i32) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error); + let raw_fd = + ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error); if error.is_null() { - Ok(ret) + let fd = OwnedFd::from_raw_fd(raw_fd); + Ok(fd) } else { Err(from_glib_full(error)) } From 046d4f54ae34804e7405f776306151e63bb63c15 Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval R Date: Wed, 28 Dec 2022 10:28:31 +0100 Subject: [PATCH 3/3] subprocess_launcher: Take owned fds --- gio/src/subprocess_launcher.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/gio/src/subprocess_launcher.rs b/gio/src/subprocess_launcher.rs index ba81437ba160..25956d22e830 100644 --- a/gio/src/subprocess_launcher.rs +++ b/gio/src/subprocess_launcher.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. #[cfg(any(unix, all(docsrs, unix)))] -use std::os::unix::io::IntoRawFd; +use std::os::unix::io::{AsFd, AsRawFd, IntoRawFd, OwnedFd}; use glib::translate::*; @@ -26,40 +26,41 @@ impl SubprocessLauncher { #[cfg(unix)] #[cfg_attr(docsrs, doc(cfg(unix)))] #[doc(alias = "g_subprocess_launcher_take_fd")] - pub fn take_fd(&self, source_fd: impl IntoRawFd, target_fd: impl IntoRawFd) { + pub fn take_fd(&self, source_fd: OwnedFd, target_fd: impl AsFd) { + let source_raw_fd = source_fd.into_raw_fd(); + let target_raw_fd = target_fd.as_fd().as_raw_fd(); unsafe { - ffi::g_subprocess_launcher_take_fd( - self.to_glib_none().0, - source_fd.into_raw_fd(), - target_fd.into_raw_fd(), - ); + ffi::g_subprocess_launcher_take_fd(self.to_glib_none().0, source_raw_fd, target_raw_fd); } } #[cfg(unix)] #[cfg_attr(docsrs, doc(cfg(unix)))] #[doc(alias = "g_subprocess_launcher_take_stderr_fd")] - pub fn take_stderr_fd(&self, fd: impl IntoRawFd) { + pub fn take_stderr_fd(&self, fd: Option) { unsafe { - ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, fd.into_raw_fd()); + let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd()); + ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, raw_fd); } } #[cfg(unix)] #[cfg_attr(docsrs, doc(cfg(unix)))] #[doc(alias = "g_subprocess_launcher_take_stdin_fd")] - pub fn take_stdin_fd(&self, fd: impl IntoRawFd) { + pub fn take_stdin_fd(&self, fd: Option) { + let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd()); unsafe { - ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, fd.into_raw_fd()); + ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, raw_fd); } } #[cfg(unix)] #[cfg_attr(docsrs, doc(cfg(unix)))] #[doc(alias = "g_subprocess_launcher_take_stdout_fd")] - pub fn take_stdout_fd(&self, fd: impl IntoRawFd) { + pub fn take_stdout_fd(&self, fd: Option) { + let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd()); unsafe { - ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, fd.into_raw_fd()); + ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, raw_fd); } } }