Skip to content

Commit 66a79a1

Browse files
committed
Use AsFd in copy_file_range
1 parent 060e2ab commit 66a79a1

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1010
- Added `nix::ucontext` module on `aarch64-unknown-linux-gnu`.
1111
(#[1662](https://github.com/nix-rust/nix/pull/1662))
1212
- Exposed `copy_file_range` on FreeBSD
13-
(#[???](https://github.com/nix-rust/nix/pull/???))
13+
(#[1906](https://github.com/nix-rust/nix/pull/1906))
1414

1515
### Changed
1616

1717
- The MSRV is now 1.63
1818
([#1862](https://github.com/nix-rust/nix/pull/1862))
1919

20+
- Implemented I/O safety. Many public functions argument and return types have
21+
changed:
22+
| Original Type | New Type |
23+
| ------------- | --------------------- |
24+
| AsRawFd | AsFd |
25+
| RawFd | BorrowedFd or OwnedFd |
26+
27+
(#[1906](https://github.com/nix-rust/nix/pull/1906))
28+
2029
### Fixed
2130
### Removed
2231

src/fcntl.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ use std::ffi::OsString;
55
use std::os::raw;
66
use std::os::unix::ffi::OsStringExt;
77
use std::os::unix::io::RawFd;
8+
// For splice and copy_file_range
9+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
10+
use std::{
11+
os::unix::io::{AsRawFd, AsFd},
12+
ptr
13+
};
814

915
#[cfg(feature = "fs")]
1016
use crate::{sys::stat::Mode, NixPath, Result};
11-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
12-
use std::ptr; // For splice and copy_file_range
1317

1418
#[cfg(any(
1519
target_os = "linux",
@@ -593,10 +597,10 @@ type type_of_off = libc::off_t;
593597
/// On successful completion the number of bytes actually copied will be
594598
/// returned.
595599
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
596-
pub fn copy_file_range(
597-
fd_in: RawFd,
600+
pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
601+
fd_in: Fd1,
598602
off_in: Option<&mut type_of_off>,
599-
fd_out: RawFd,
603+
fd_out: Fd2,
600604
off_out: Option<&mut type_of_off>,
601605
len: usize,
602606
) -> Result<usize> {
@@ -609,9 +613,9 @@ pub fn copy_file_range(
609613

610614
let ret = unsafe {
611615
libc::copy_file_range(
612-
fd_in,
616+
fd_in.as_fd().as_raw_fd(),
613617
off_in,
614-
fd_out,
618+
fd_out.as_fd().as_raw_fd(),
615619
off_out,
616620
len,
617621
0,

test/test_fcntl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn test_readlink() {
243243
// QEMU does not support copy_file_range. Skip under qemu
244244
#[cfg_attr(qemu, ignore)]
245245
fn test_copy_file_range() {
246-
use std::os::unix::io::AsRawFd;
246+
use std::os::fd::AsFd;
247247
use nix::fcntl::copy_file_range;
248248

249249
const CONTENTS: &[u8] = b"foobarbaz";
@@ -256,9 +256,9 @@ fn test_copy_file_range() {
256256

257257
let mut from_offset: i64 = 3;
258258
copy_file_range(
259-
tmp1.as_raw_fd(),
259+
tmp1.as_fd(),
260260
Some(&mut from_offset),
261-
tmp2.as_raw_fd(),
261+
tmp2.as_fd(),
262262
None,
263263
3,
264264
)

0 commit comments

Comments
 (0)