Skip to content

Commit 991dfaf

Browse files
committed
Port the symlink_hard_link test from Rust.
1 parent 84d4a26 commit 991dfaf

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/fs.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,3 +1395,61 @@ fn metadata_access_times() {
13951395
}
13961396
}
13971397
}
1398+
1399+
/// Test creating hard links to symlinks.
1400+
#[test]
1401+
fn symlink_hard_link() {
1402+
let tmpdir = tmpdir();
1403+
if !got_symlink_permission(&tmpdir) {
1404+
return;
1405+
}
1406+
1407+
// Create "file", a file.
1408+
check!(tmpdir.create("file"));
1409+
1410+
// Create "symlink", a symlink to "file".
1411+
check!(symlink_file("file", &tmpdir, "symlink"));
1412+
1413+
// Create "hard_link", a hard link to "symlink".
1414+
check!(tmpdir.hard_link("symlink", &tmpdir, "hard_link"));
1415+
1416+
// "hard_link" should appear as a symlink.
1417+
assert!(check!(tmpdir.symlink_metadata("hard_link"))
1418+
.file_type()
1419+
.is_symlink());
1420+
1421+
// We sould be able to open "file" via any of the above names.
1422+
let _ = check!(tmpdir.open("file"));
1423+
assert!(tmpdir.open("file.renamed").is_err());
1424+
let _ = check!(tmpdir.open("symlink"));
1425+
let _ = check!(tmpdir.open("hard_link"));
1426+
1427+
// Rename "file" to "file.renamed".
1428+
check!(tmpdir.rename("file", &tmpdir, "file.renamed"));
1429+
1430+
// Now, the symlink and the hard link should be dangling.
1431+
assert!(tmpdir.open("file").is_err());
1432+
let _ = check!(tmpdir.open("file.renamed"));
1433+
assert!(tmpdir.open("symlink").is_err());
1434+
assert!(tmpdir.open("hard_link").is_err());
1435+
1436+
// The symlink and the hard link should both still point to "file".
1437+
assert!(tmpdir.read_link("file").is_err());
1438+
assert!(tmpdir.read_link("file.renamed").is_err());
1439+
assert_eq!(check!(tmpdir.read_link("symlink")), Path::new("file"));
1440+
assert_eq!(check!(tmpdir.read_link("hard_link")), Path::new("file"));
1441+
1442+
// Remove "file.renamed".
1443+
check!(tmpdir.remove_file("file.renamed"));
1444+
1445+
// Now, we can't open the file by any name.
1446+
assert!(tmpdir.open("file").is_err());
1447+
assert!(tmpdir.open("file.renamed").is_err());
1448+
assert!(tmpdir.open("symlink").is_err());
1449+
assert!(tmpdir.open("hard_link").is_err());
1450+
1451+
// "hard_link" should still appear as a symlink.
1452+
assert!(check!(tmpdir.symlink_metadata("hard_link"))
1453+
.file_type()
1454+
.is_symlink());
1455+
}

0 commit comments

Comments
 (0)