@@ -12,8 +12,15 @@ use crate::sys::time::SystemTime;
12
12
use crate :: sys:: { cvt, cvt_r} ;
13
13
use crate :: sys_common:: { AsInner , FromInner } ;
14
14
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
+
15
20
use libc:: { c_int, mode_t} ;
16
21
22
+ #[ cfg( any( target_os = "macos" , all( target_os = "linux" , target_env = "gnu" ) ) ) ]
23
+ use libc:: c_char;
17
24
#[ cfg( any( target_os = "linux" , target_os = "emscripten" , target_os = "android" ) ) ]
18
25
use libc:: dirfd;
19
26
#[ cfg( any( target_os = "linux" , target_os = "emscripten" ) ) ]
@@ -92,7 +99,7 @@ cfg_has_statx! {{
92
99
// Default `stat64` contains no creation time.
93
100
unsafe fn try_statx(
94
101
fd: c_int,
95
- path: * const libc :: c_char,
102
+ path: * const c_char,
96
103
flags: i32 ,
97
104
mask: u32 ,
98
105
) -> Option <io:: Result <FileAttr >> {
@@ -107,7 +114,7 @@ cfg_has_statx! {{
107
114
syscall! {
108
115
fn statx(
109
116
fd: c_int,
110
- pathname: * const libc :: c_char,
117
+ pathname: * const c_char,
111
118
flags: c_int,
112
119
mask: libc:: c_uint,
113
120
statxbuf: * mut libc:: statx
@@ -756,7 +763,7 @@ impl File {
756
763
cfg_has_statx ! {
757
764
if let Some ( ret) = unsafe { try_statx(
758
765
fd,
759
- b"\0 " as * const _ as * const libc :: c_char,
766
+ b"\0 " as * const _ as * const c_char,
760
767
libc:: AT_EMPTY_PATH | libc:: AT_STATX_SYNC_AS_STAT ,
761
768
libc:: STATX_ALL ,
762
769
) } {
@@ -1087,15 +1094,28 @@ pub fn link(original: &Path, link: &Path) -> io::Result<()> {
1087
1094
let link = cstr ( link) ?;
1088
1095
cfg_if:: cfg_if! {
1089
1096
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.
1094
1102
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
+ } ;
1095
1116
} 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.
1099
1119
cvt( unsafe { libc:: linkat( libc:: AT_FDCWD , original. as_ptr( ) , libc:: AT_FDCWD , link. as_ptr( ) , 0 ) } ) ?;
1100
1120
}
1101
1121
}
@@ -1278,7 +1298,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1278
1298
fn fclonefileat(
1279
1299
srcfd: libc:: c_int,
1280
1300
dst_dirfd: libc:: c_int,
1281
- dst: * const libc :: c_char,
1301
+ dst: * const c_char,
1282
1302
flags: libc:: c_int
1283
1303
) -> libc:: c_int
1284
1304
}
0 commit comments