Skip to content

Commit

Permalink
Use AsFd in copy_file_range
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed Dec 4, 2022
1 parent 060e2ab commit 66a79a1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 11 additions & 7 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<Fd1: AsFd, Fd2: AsFd>(
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<usize> {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::fd::AsFd;
use nix::fcntl::copy_file_range;

const CONTENTS: &[u8] = b"foobarbaz";
Expand All @@ -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,
)
Expand Down

0 comments on commit 66a79a1

Please sign in to comment.