Skip to content

Commit 033becf

Browse files
committed
Auto merge of #123485 - madsmtm:use-libc-copyfile, r=joboet
macOS: Use `libc` definitions for copyfile `COPYFILE_ALL` is not yet exposed in `libc`, but the rest of what we need is, so use those definitions instead of manually defining them. The definitions were added in rust-lang/libc#2667 and rust-lang/libc#3346.
2 parents ff24ef9 + 3fe5839 commit 033becf

File tree

1 file changed

+10
-39
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+10
-39
lines changed

Diff for: library/std/src/sys/pal/unix/fs.rs

+10-39
Original file line numberDiff line numberDiff line change
@@ -1847,47 +1847,17 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
18471847
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
18481848
use crate::sync::atomic::{AtomicBool, Ordering};
18491849

1850-
const COPYFILE_ACL: u32 = 1 << 0;
1851-
const COPYFILE_STAT: u32 = 1 << 1;
1852-
const COPYFILE_XATTR: u32 = 1 << 2;
1853-
const COPYFILE_DATA: u32 = 1 << 3;
1854-
1855-
const COPYFILE_SECURITY: u32 = COPYFILE_STAT | COPYFILE_ACL;
1856-
const COPYFILE_METADATA: u32 = COPYFILE_SECURITY | COPYFILE_XATTR;
1857-
const COPYFILE_ALL: u32 = COPYFILE_METADATA | COPYFILE_DATA;
1858-
1859-
const COPYFILE_STATE_COPIED: u32 = 8;
1860-
1861-
#[allow(non_camel_case_types)]
1862-
type copyfile_state_t = *mut libc::c_void;
1863-
#[allow(non_camel_case_types)]
1864-
type copyfile_flags_t = u32;
1865-
1866-
extern "C" {
1867-
fn fcopyfile(
1868-
from: libc::c_int,
1869-
to: libc::c_int,
1870-
state: copyfile_state_t,
1871-
flags: copyfile_flags_t,
1872-
) -> libc::c_int;
1873-
fn copyfile_state_alloc() -> copyfile_state_t;
1874-
fn copyfile_state_free(state: copyfile_state_t) -> libc::c_int;
1875-
fn copyfile_state_get(
1876-
state: copyfile_state_t,
1877-
flag: u32,
1878-
dst: *mut libc::c_void,
1879-
) -> libc::c_int;
1880-
}
1881-
1882-
struct FreeOnDrop(copyfile_state_t);
1850+
const COPYFILE_ALL: libc::copyfile_flags_t = libc::COPYFILE_METADATA | libc::COPYFILE_DATA;
1851+
1852+
struct FreeOnDrop(libc::copyfile_state_t);
18831853
impl Drop for FreeOnDrop {
18841854
fn drop(&mut self) {
18851855
// The code below ensures that `FreeOnDrop` is never a null pointer
18861856
unsafe {
18871857
// `copyfile_state_free` returns -1 if the `to` or `from` files
18881858
// cannot be closed. However, this is not considered this an
18891859
// error.
1890-
copyfile_state_free(self.0);
1860+
libc::copyfile_state_free(self.0);
18911861
}
18921862
}
18931863
}
@@ -1896,6 +1866,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
18961866
// We store the availability in a global to avoid unnecessary syscalls
18971867
static HAS_FCLONEFILEAT: AtomicBool = AtomicBool::new(true);
18981868
syscall! {
1869+
// Mirrors `libc::fclonefileat`
18991870
fn fclonefileat(
19001871
srcfd: libc::c_int,
19011872
dst_dirfd: libc::c_int,
@@ -1932,22 +1903,22 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
19321903
// We ensure that `FreeOnDrop` never contains a null pointer so it is
19331904
// always safe to call `copyfile_state_free`
19341905
let state = unsafe {
1935-
let state = copyfile_state_alloc();
1906+
let state = libc::copyfile_state_alloc();
19361907
if state.is_null() {
19371908
return Err(crate::io::Error::last_os_error());
19381909
}
19391910
FreeOnDrop(state)
19401911
};
19411912

1942-
let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { COPYFILE_DATA };
1913+
let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { libc::COPYFILE_DATA };
19431914

1944-
cvt(unsafe { fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
1915+
cvt(unsafe { libc::fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
19451916

19461917
let mut bytes_copied: libc::off_t = 0;
19471918
cvt(unsafe {
1948-
copyfile_state_get(
1919+
libc::copyfile_state_get(
19491920
state.0,
1950-
COPYFILE_STATE_COPIED,
1921+
libc::COPYFILE_STATE_COPIED as u32,
19511922
core::ptr::addr_of_mut!(bytes_copied) as *mut libc::c_void,
19521923
)
19531924
})?;

0 commit comments

Comments
 (0)