@@ -12,8 +12,15 @@ use crate::sys::time::SystemTime;
1212use crate :: sys:: { cvt, cvt_r} ;
1313use crate :: sys_common:: { AsInner , FromInner } ;
1414
15+ #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ]
16+ use crate :: sys:: weak:: syscall;
17+ #[ cfg( target_os = "macos" ) ]
18+ use crate :: sys:: weak:: { syscall, weak} ;
19+
1520use libc:: { c_int, mode_t} ;
1621
22+ #[ cfg( any( target_os = "macos" , all( target_os = "linux" , target_env = "gnu" ) ) ) ]
23+ use libc:: c_char;
1724#[ cfg( any( target_os = "linux" , target_os = "emscripten" , target_os = "android" ) ) ]
1825use libc:: dirfd;
1926#[ cfg( any( target_os = "linux" , target_os = "emscripten" ) ) ]
@@ -92,7 +99,7 @@ cfg_has_statx! {{
9299 // Default `stat64` contains no creation time.
93100 unsafe fn try_statx(
94101 fd: c_int,
95- path: * const libc :: c_char,
102+ path: * const c_char,
96103 flags: i32 ,
97104 mask: u32 ,
98105 ) -> Option <io:: Result <FileAttr >> {
@@ -107,7 +114,7 @@ cfg_has_statx! {{
107114 syscall! {
108115 fn statx(
109116 fd: c_int,
110- pathname: * const libc :: c_char,
117+ pathname: * const c_char,
111118 flags: c_int,
112119 mask: libc:: c_uint,
113120 statxbuf: * mut libc:: statx
@@ -756,7 +763,7 @@ impl File {
756763 cfg_has_statx ! {
757764 if let Some ( ret) = unsafe { try_statx(
758765 fd,
759- b"\0 " as * const _ as * const libc :: c_char,
766+ b"\0 " as * const _ as * const c_char,
760767 libc:: AT_EMPTY_PATH | libc:: AT_STATX_SYNC_AS_STAT ,
761768 libc:: STATX_ALL ,
762769 ) } {
@@ -1087,15 +1094,28 @@ pub fn link(original: &Path, link: &Path) -> io::Result<()> {
10871094 let link = cstr ( link) ?;
10881095 cfg_if:: cfg_if! {
10891096 if #[ cfg( any( target_os = "vxworks" , target_os = "redox" , target_os = "android" ) ) ] {
1090- // VxWorks, Redox, and old versions of Android lack `linkat`, so use
1091- // `link` instead. POSIX leaves it implementation-defined whether
1092- // `link` follows symlinks, so rely on the `symlink_hard_link` test
1093- // in library/std/src/fs/tests.rs to check the behavior.
1097+ // VxWorks and Redox lack `linkat`, so use `link` instead. POSIX leaves
1098+ // it implementation-defined whether `link` follows symlinks, so rely on the
1099+ // `symlink_hard_link` test in library/std/src/fs/tests.rs to check the behavior.
1100+ // Android has `linkat` on newer versions, but we happen to know `link`
1101+ // always has the correct behavior, so it's here as well.
10941102 cvt( unsafe { libc:: link( original. as_ptr( ) , link. as_ptr( ) ) } ) ?;
1103+ } else if #[ cfg( target_os = "macos" ) ] {
1104+ // On MacOS, older versions (<=10.9) lack support for linkat while newer
1105+ // versions have it. We want to use linkat if it is available, so we use weak!
1106+ // to check. `linkat` is preferable to `link` ecause it gives us a flag to
1107+ // specify how symlinks should be handled. We pass 0 as the flags argument,
1108+ // meaning it shouldn't follow symlinks.
1109+ weak!( fn linkat( c_int, * const c_char, c_int, * const c_char, c_int) -> c_int) ;
1110+
1111+ if let Some ( f) = linkat. get( ) {
1112+ cvt( unsafe { f( libc:: AT_FDCWD , original. as_ptr( ) , libc:: AT_FDCWD , link. as_ptr( ) , 0 ) } ) ?;
1113+ } else {
1114+ cvt( unsafe { libc:: link( original. as_ptr( ) , link. as_ptr( ) ) } ) ?;
1115+ } ;
10951116 } else {
1096- // Use `linkat` with `AT_FDCWD` instead of `link` as `linkat` gives
1097- // us a flag to specify how symlinks should be handled. Pass 0 as
1098- // the flags argument, meaning don't follow symlinks.
1117+ // Where we can, use `linkat` instead of `link`; see the comment above
1118+ // this one for details on why.
10991119 cvt( unsafe { libc:: linkat( libc:: AT_FDCWD , original. as_ptr( ) , libc:: AT_FDCWD , link. as_ptr( ) , 0 ) } ) ?;
11001120 }
11011121 }
@@ -1278,7 +1298,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
12781298 fn fclonefileat(
12791299 srcfd: libc:: c_int,
12801300 dst_dirfd: libc:: c_int,
1281- dst: * const libc :: c_char,
1301+ dst: * const c_char,
12821302 flags: libc:: c_int
12831303 ) -> libc:: c_int
12841304 }
0 commit comments