Skip to content

Commit 3fe5839

Browse files
committed
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.
1 parent 385fa9d commit 3fe5839

File tree

1 file changed

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

1 file changed

+10
-39
lines changed

library/std/src/sys/pal/unix/fs.rs

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

1820-
const COPYFILE_ACL: u32 = 1 << 0;
1821-
const COPYFILE_STAT: u32 = 1 << 1;
1822-
const COPYFILE_XATTR: u32 = 1 << 2;
1823-
const COPYFILE_DATA: u32 = 1 << 3;
1824-
1825-
const COPYFILE_SECURITY: u32 = COPYFILE_STAT | COPYFILE_ACL;
1826-
const COPYFILE_METADATA: u32 = COPYFILE_SECURITY | COPYFILE_XATTR;
1827-
const COPYFILE_ALL: u32 = COPYFILE_METADATA | COPYFILE_DATA;
1828-
1829-
const COPYFILE_STATE_COPIED: u32 = 8;
1830-
1831-
#[allow(non_camel_case_types)]
1832-
type copyfile_state_t = *mut libc::c_void;
1833-
#[allow(non_camel_case_types)]
1834-
type copyfile_flags_t = u32;
1835-
1836-
extern "C" {
1837-
fn fcopyfile(
1838-
from: libc::c_int,
1839-
to: libc::c_int,
1840-
state: copyfile_state_t,
1841-
flags: copyfile_flags_t,
1842-
) -> libc::c_int;
1843-
fn copyfile_state_alloc() -> copyfile_state_t;
1844-
fn copyfile_state_free(state: copyfile_state_t) -> libc::c_int;
1845-
fn copyfile_state_get(
1846-
state: copyfile_state_t,
1847-
flag: u32,
1848-
dst: *mut libc::c_void,
1849-
) -> libc::c_int;
1850-
}
1851-
1852-
struct FreeOnDrop(copyfile_state_t);
1820+
const COPYFILE_ALL: libc::copyfile_flags_t = libc::COPYFILE_METADATA | libc::COPYFILE_DATA;
1821+
1822+
struct FreeOnDrop(libc::copyfile_state_t);
18531823
impl Drop for FreeOnDrop {
18541824
fn drop(&mut self) {
18551825
// The code below ensures that `FreeOnDrop` is never a null pointer
18561826
unsafe {
18571827
// `copyfile_state_free` returns -1 if the `to` or `from` files
18581828
// cannot be closed. However, this is not considered this an
18591829
// error.
1860-
copyfile_state_free(self.0);
1830+
libc::copyfile_state_free(self.0);
18611831
}
18621832
}
18631833
}
@@ -1866,6 +1836,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
18661836
// We store the availability in a global to avoid unnecessary syscalls
18671837
static HAS_FCLONEFILEAT: AtomicBool = AtomicBool::new(true);
18681838
syscall! {
1839+
// Mirrors `libc::fclonefileat`
18691840
fn fclonefileat(
18701841
srcfd: libc::c_int,
18711842
dst_dirfd: libc::c_int,
@@ -1902,22 +1873,22 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
19021873
// We ensure that `FreeOnDrop` never contains a null pointer so it is
19031874
// always safe to call `copyfile_state_free`
19041875
let state = unsafe {
1905-
let state = copyfile_state_alloc();
1876+
let state = libc::copyfile_state_alloc();
19061877
if state.is_null() {
19071878
return Err(crate::io::Error::last_os_error());
19081879
}
19091880
FreeOnDrop(state)
19101881
};
19111882

1912-
let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { COPYFILE_DATA };
1883+
let flags = if writer_metadata.is_file() { COPYFILE_ALL } else { libc::COPYFILE_DATA };
19131884

1914-
cvt(unsafe { fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
1885+
cvt(unsafe { libc::fcopyfile(reader.as_raw_fd(), writer.as_raw_fd(), state.0, flags) })?;
19151886

19161887
let mut bytes_copied: libc::off_t = 0;
19171888
cvt(unsafe {
1918-
copyfile_state_get(
1889+
libc::copyfile_state_get(
19191890
state.0,
1920-
COPYFILE_STATE_COPIED,
1891+
libc::COPYFILE_STATE_COPIED as u32,
19211892
core::ptr::addr_of_mut!(bytes_copied) as *mut libc::c_void,
19221893
)
19231894
})?;

0 commit comments

Comments
 (0)