Skip to content

Commit ce00b3e

Browse files
committed
Use link on platforms which lack linkat.
1 parent 23a5c21 commit ce00b3e

File tree

1 file changed

+14
-4
lines changed
  • library/std/src/sys/unix

1 file changed

+14
-4
lines changed

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,20 @@ pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
10671067
pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
10681068
let src = cstr(src)?;
10691069
let dst = cstr(dst)?;
1070-
// Use `linkat` with `AT_FDCWD` instead of `link` as `link` leaves it
1071-
// implementation-defined whether it follows symlinks. Pass 0 as the
1072-
// `linkat` flags argument so that we don't follow symlinks.
1073-
cvt(unsafe { libc::linkat(libc::AT_FDCWD, src.as_ptr(), libc::AT_FDCWD, dst.as_ptr(), 0) })?;
1070+
cfg_if::cfg_if! {
1071+
if #[cfg(any(target_os = "vxworks", target_os = "redox"))] {
1072+
// VxWorks and Redox lack `linkat`, so use `link` instead. POSIX
1073+
// leaves it implementation-defined whether `link` follows symlinks,
1074+
// so rely on the `symlink_hard_link` test in
1075+
// library/std/src/fs/tests.rs to check the behavior.
1076+
cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })?;
1077+
} else {
1078+
// Use `linkat` with `AT_FDCWD` instead of `link` as `linkat` gives
1079+
// us a flag to specify how symlinks should be handled. Pass 0 as
1080+
// the flags argument, meaning don't follow symlinks.
1081+
cvt(unsafe { libc::linkat(libc::AT_FDCWD, src.as_ptr(), libc::AT_FDCWD, dst.as_ptr(), 0) })?;
1082+
}
1083+
}
10741084
Ok(())
10751085
}
10761086

0 commit comments

Comments
 (0)