From fbf64704409fa4e54faaa37afa5eaac34c127c53 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sun, 4 Dec 2022 08:35:35 -0700 Subject: [PATCH] Use AsFd in copy_file_range --- CHANGELOG.md | 11 ++++++++++- src/fcntl.rs | 18 +++++++++++------- test/test_fcntl.rs | 6 +++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89e47772ba..0c4c63e2b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,13 +10,22 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Added `nix::ucontext` module on `aarch64-unknown-linux-gnu`. (#[1662](https://github.com/nix-rust/nix/pull/1662)) - Exposed `copy_file_range` on FreeBSD - (#[???](https://github.com/nix-rust/nix/pull/???)) + (#[1906](https://github.com/nix-rust/nix/pull/1906)) ### Changed - The MSRV is now 1.63 ([#1862](https://github.com/nix-rust/nix/pull/1862)) +- Implemented I/O safety. Many public functions argument and return types have + changed: + | Original Type | New Type | + | ------------- | --------------------- | + | AsRawFd | AsFd | + | RawFd | BorrowedFd or OwnedFd | + + (#[1906](https://github.com/nix-rust/nix/pull/1906)) + ### Fixed ### Removed diff --git a/src/fcntl.rs b/src/fcntl.rs index c4c1e92f82..0c7066a8bb 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -5,11 +5,15 @@ use std::ffi::OsString; use std::os::raw; use std::os::unix::ffi::OsStringExt; use std::os::unix::io::RawFd; +// For splice and copy_file_range +#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))] +use std::{ + os::unix::io::{AsRawFd, AsFd}, + ptr +}; #[cfg(feature = "fs")] use crate::{sys::stat::Mode, NixPath, Result}; -#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))] -use std::ptr; // For splice and copy_file_range #[cfg(any( target_os = "linux", @@ -593,10 +597,10 @@ type type_of_off = libc::off_t; /// On successful completion the number of bytes actually copied will be /// returned. #[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))] -pub fn copy_file_range( - fd_in: RawFd, +pub fn copy_file_range( + fd_in: Fd1, off_in: Option<&mut type_of_off>, - fd_out: RawFd, + fd_out: Fd2, off_out: Option<&mut type_of_off>, len: usize, ) -> Result { @@ -609,9 +613,9 @@ pub fn copy_file_range( let ret = unsafe { libc::copy_file_range( - fd_in, + fd_in.as_fd().as_raw_fd(), off_in, - fd_out, + fd_out.as_fd().as_raw_fd(), off_out, len, 0, diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index dca6b79bfb..6df9190a8a 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -243,7 +243,7 @@ fn test_readlink() { // QEMU does not support copy_file_range. Skip under qemu #[cfg_attr(qemu, ignore)] fn test_copy_file_range() { - use std::os::unix::io::AsRawFd; + use std::os::unix::io::AsFd; use nix::fcntl::copy_file_range; const CONTENTS: &[u8] = b"foobarbaz"; @@ -256,9 +256,9 @@ fn test_copy_file_range() { let mut from_offset: i64 = 3; copy_file_range( - tmp1.as_raw_fd(), + tmp1.as_fd(), Some(&mut from_offset), - tmp2.as_raw_fd(), + tmp2.as_fd(), None, 3, )