Skip to content

Commit 0b4afa7

Browse files
committed
Use AsFd in copy_file_range
1 parent c7827ce commit 0b4afa7

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
@@ -15,7 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1515
([#1966])(https://github.com/nix-rust/nix/pull/1966)
1616
- Added `LocalPeerPid` to `nix::sys::socket::sockopt` for macOS. ([#1967](https://github.com/nix-rust/nix/pull/1967))
1717
- Exposed `copy_file_range` on FreeBSD
18-
(#[???](https://github.com/nix-rust/nix/pull/???))
18+
(#[1906](https://github.com/nix-rust/nix/pull/1906))
1919

2020
### Changed
2121

@@ -27,6 +27,15 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2727
users no longer need to manually close the file descriptors in these types.
2828
([#1921](https://github.com/nix-rust/nix/pull/1921))
2929

30+
- Implemented I/O safety. Many public functions argument and return types have
31+
changed:
32+
| Original Type | New Type |
33+
| ------------- | --------------------- |
34+
| AsRawFd | AsFd |
35+
| RawFd | BorrowedFd or OwnedFd |
36+
37+
(#[1906](https://github.com/nix-rust/nix/pull/1906))
38+
3039
### Fixed
3140
- Fix `SockaddrIn6` bug that was swapping flowinfo and scope_id byte ordering.
3241
([#1964](https://github.com/nix-rust/nix/pull/1964))

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",
@@ -627,10 +631,10 @@ type type_of_off = libc::off_t;
627631
/// On successful completion the number of bytes actually copied will be
628632
/// returned.
629633
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
630-
pub fn copy_file_range(
631-
fd_in: RawFd,
634+
pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
635+
fd_in: Fd1,
632636
off_in: Option<&mut type_of_off>,
633-
fd_out: RawFd,
637+
fd_out: Fd2,
634638
off_out: Option<&mut type_of_off>,
635639
len: usize,
636640
) -> Result<usize> {
@@ -643,9 +647,9 @@ pub fn copy_file_range(
643647

644648
let ret = unsafe {
645649
libc::copy_file_range(
646-
fd_in,
650+
fd_in.as_fd().as_raw_fd(),
647651
off_in,
648-
fd_out,
652+
fd_out.as_fd().as_raw_fd(),
649653
off_out,
650654
len,
651655
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::unix::io::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)