Skip to content

Commit 91a9f83

Browse files
committed
Define fs::hard_link to not follow symlinks.
POSIX leaves it implementation-defined whether `link` follows symlinks. In practice, for example, on Linux it does not and on FreeBSD it does. So, switch to `linkat`, so that we can pick a behavior rather than depending on OS defaults. Pick the option to not follow symlinks. This is somewhat arbitrary, but seems the less surprising choice because hard linking is a very low-level feature which requires the source and destination to be on the same mounted filesystem, and following a symbolic link could end up in a different mounted filesystem.
1 parent a78a62f commit 91a9f83

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

library/std/src/fs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,10 +1701,13 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
17011701
/// The `dst` path will be a link pointing to the `src` path. Note that systems
17021702
/// often require these two paths to both be located on the same filesystem.
17031703
///
1704+
/// If `src` names a symbolic link, it is not followed. The created hard link
1705+
/// points to the symbolic link itself.
1706+
///
17041707
/// # Platform-specific behavior
17051708
///
1706-
/// This function currently corresponds to the `link` function on Unix
1707-
/// and the `CreateHardLink` function on Windows.
1709+
/// This function currently corresponds to the `linkat` function with no flags
1710+
/// on Unix and the `CreateHardLink` function on Windows.
17081711
/// Note that, this [may change in the future][changes].
17091712
///
17101713
/// [changes]: io#platform-specific-behavior

library/std/src/fs/tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,3 +1337,54 @@ fn metadata_access_times() {
13371337
}
13381338
}
13391339
}
1340+
1341+
/// Test creating hard links to symlinks.
1342+
#[test]
1343+
fn symlink_hard_link() {
1344+
let tmpdir = tmpdir();
1345+
1346+
// Create "file", a file.
1347+
check!(fs::File::create(tmpdir.join("file")));
1348+
1349+
// Create "symlink", a symlink to "file".
1350+
check!(symlink_file("file", tmpdir.join("symlink")));
1351+
1352+
// Create "hard_link", a hard link to "symlink".
1353+
check!(fs::hard_link(tmpdir.join("symlink"), tmpdir.join("hard_link")));
1354+
1355+
// "hard_link" should appear as a symlink.
1356+
assert!(check!(fs::symlink_metadata(tmpdir.join("hard_link"))).file_type().is_symlink());
1357+
1358+
// We sould be able to open "file" via any of the above names.
1359+
let _ = check!(fs::File::open(tmpdir.join("file")));
1360+
assert!(fs::File::open(tmpdir.join("file.renamed")).is_err());
1361+
let _ = check!(fs::File::open(tmpdir.join("symlink")));
1362+
let _ = check!(fs::File::open(tmpdir.join("hard_link")));
1363+
1364+
// Rename "file" to "file.renamed".
1365+
check!(fs::rename(tmpdir.join("file"), tmpdir.join("file.renamed")));
1366+
1367+
// Now, the symlink and the hard link should be dangling.
1368+
assert!(fs::File::open(tmpdir.join("file")).is_err());
1369+
let _ = check!(fs::File::open(tmpdir.join("file.renamed")));
1370+
assert!(fs::File::open(tmpdir.join("symlink")).is_err());
1371+
assert!(fs::File::open(tmpdir.join("hard_link")).is_err());
1372+
1373+
// The symlink and the hard link should both still point to "file".
1374+
assert!(fs::read_link(tmpdir.join("file")).is_err());
1375+
assert!(fs::read_link(tmpdir.join("file.renamed")).is_err());
1376+
assert_eq!(check!(fs::read_link(tmpdir.join("symlink"))), Path::new("file"));
1377+
assert_eq!(check!(fs::read_link(tmpdir.join("hard_link"))), Path::new("file"));
1378+
1379+
// Remove "file.renamed".
1380+
check!(fs::remove_file(tmpdir.join("file.renamed")));
1381+
1382+
// Now, we can't open the file by any name.
1383+
assert!(fs::File::open(tmpdir.join("file")).is_err());
1384+
assert!(fs::File::open(tmpdir.join("file.renamed")).is_err());
1385+
assert!(fs::File::open(tmpdir.join("symlink")).is_err());
1386+
assert!(fs::File::open(tmpdir.join("hard_link")).is_err());
1387+
1388+
// "hard_link" should still appear as a symlink.
1389+
assert!(check!(fs::symlink_metadata(tmpdir.join("hard_link"))).file_type().is_symlink());
1390+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,10 @@ 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-
cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })?;
1070+
// Use `linkat` with `AT_FDCWD` instead of `link` as `link` leaves it
1071+
// implmentation-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) })?;
10711074
Ok(())
10721075
}
10731076

0 commit comments

Comments
 (0)