Skip to content

Commit ed12887

Browse files
sylvestrecakebaker
andcommitted
install: implement option -C
Co-authored-by: Daniel Hofstetter <[email protected]>
1 parent 507e1cc commit ed12887

File tree

2 files changed

+227
-8
lines changed

2 files changed

+227
-8
lines changed

src/uu/install/src/install.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,36 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
999999
Ok(())
10001000
}
10011001

1002+
/// Check if a file needs to be copied due to ownership differences when no explicit group is specified.
1003+
/// Returns true if the destination file's ownership would differ from what it should be after installation.
1004+
#[cfg(not(target_os = "windows"))]
1005+
fn needs_copy_for_ownership(to: &Path, to_meta: &fs::Metadata) -> bool {
1006+
use std::os::unix::fs::MetadataExt;
1007+
1008+
// Check if the destination file's owner differs from the effective user ID
1009+
if to_meta.uid() != geteuid() {
1010+
return true;
1011+
}
1012+
1013+
// For group, we need to determine what the group would be after installation
1014+
// If no group is specified, the behavior depends on the directory:
1015+
// - If the directory has setgid bit, the file inherits the directory's group
1016+
// - Otherwise, the file gets the user's effective group
1017+
let expected_gid = to
1018+
.parent()
1019+
.and_then(|parent| metadata(parent).ok())
1020+
.filter(|parent_meta| parent_meta.mode() & 0o2000 != 0)
1021+
.map(|parent_meta| parent_meta.gid())
1022+
.unwrap_or(getegid());
1023+
1024+
to_meta.gid() != expected_gid
1025+
}
1026+
1027+
#[cfg(target_os = "windows")]
1028+
fn needs_copy_for_ownership(_to: &Path, _to_meta: &std::fs::Metadata) -> bool {
1029+
false
1030+
}
1031+
10021032
/// Return true if a file is necessary to copy. This is the case when:
10031033
///
10041034
/// - _from_ or _to_ is nonexistent;
@@ -1029,6 +1059,13 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
10291059
return Ok(true);
10301060
};
10311061

1062+
// Check if the destination is a symlink (should always be replaced)
1063+
if let Ok(to_symlink_meta) = fs::symlink_metadata(to) {
1064+
if to_symlink_meta.file_type().is_symlink() {
1065+
return Ok(true);
1066+
}
1067+
}
1068+
10321069
// Define special file mode bits (setuid, setgid, sticky).
10331070
let extra_mode: u32 = 0o7000;
10341071
// Define all file mode bits (including permissions).
@@ -1037,7 +1074,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
10371074

10381075
// Check if any special mode bits are set in the specified mode,
10391076
// source file mode, or destination file mode.
1040-
if b.specified_mode.unwrap_or(0) & extra_mode != 0
1077+
if b.mode() & extra_mode != 0
10411078
|| from_meta.mode() & extra_mode != 0
10421079
|| to_meta.mode() & extra_mode != 0
10431080
{
@@ -1078,13 +1115,8 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
10781115
if group_id != to_meta.gid() {
10791116
return Ok(true);
10801117
}
1081-
} else {
1082-
#[cfg(not(target_os = "windows"))]
1083-
// Check if the destination file's owner or group
1084-
// differs from the effective user/group ID of the process.
1085-
if to_meta.uid() != geteuid() || to_meta.gid() != getegid() {
1086-
return Ok(true);
1087-
}
1118+
} else if needs_copy_for_ownership(to, &to_meta) {
1119+
return Ok(true);
10881120
}
10891121

10901122
// Check if the contents of the source and destination files differ.

tests/by-util/test_install.rs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,193 @@ fn test_install_compare_option() {
16201620
.stderr_contains("Options --compare and --strip are mutually exclusive");
16211621
}
16221622

1623+
#[test]
1624+
#[cfg(not(target_os = "openbsd"))]
1625+
fn test_install_compare_basic() {
1626+
let scene = TestScenario::new(util_name!());
1627+
let at = &scene.fixtures;
1628+
1629+
let source = "source_file";
1630+
let dest = "dest_file";
1631+
1632+
at.write(source, "test content");
1633+
1634+
// First install should copy
1635+
scene
1636+
.ucmd()
1637+
.args(&["-Cv", "-m644", source, dest])
1638+
.succeeds()
1639+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1640+
1641+
// Second install with same mode should be no-op (compare works)
1642+
scene
1643+
.ucmd()
1644+
.args(&["-Cv", "-m644", source, dest])
1645+
.succeeds()
1646+
.no_stdout();
1647+
1648+
// Test that compare works correctly when content actually differs
1649+
let source2 = "source2";
1650+
at.write(source2, "different content");
1651+
1652+
scene
1653+
.ucmd()
1654+
.args(&["-Cv", "-m644", source2, dest])
1655+
.succeeds()
1656+
.stdout_contains("removed")
1657+
.stdout_contains(format!("'{source2}' -> '{dest}'"));
1658+
1659+
// Second install should be no-op since content is now identical
1660+
scene
1661+
.ucmd()
1662+
.args(&["-Cv", "-m644", source2, dest])
1663+
.succeeds()
1664+
.no_stdout();
1665+
}
1666+
1667+
#[test]
1668+
#[cfg(not(any(target_os = "openbsd", target_os = "freebsd")))]
1669+
fn test_install_compare_special_mode_bits() {
1670+
let scene = TestScenario::new(util_name!());
1671+
let at = &scene.fixtures;
1672+
1673+
let source = "source_file";
1674+
let dest = "dest_file";
1675+
1676+
at.write(source, "test content");
1677+
1678+
// Special mode bits - setgid (tests the core bug fix)
1679+
// When setgid bit is set, -C should be ignored (always copy)
1680+
// This tests the bug where b.specified_mode.unwrap_or(0) was used instead of b.mode()
1681+
scene
1682+
.ucmd()
1683+
.args(&["-Cv", "-m2755", source, dest])
1684+
.succeeds()
1685+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1686+
1687+
// Second install with same setgid mode should ALSO copy (not skip)
1688+
// because -C option should be ignored when special mode bits are present
1689+
scene
1690+
.ucmd()
1691+
.args(&["-Cv", "-m2755", source, dest])
1692+
.succeeds()
1693+
.stdout_contains("removed")
1694+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1695+
1696+
// Special mode bits - setuid
1697+
scene
1698+
.ucmd()
1699+
.args(&["-Cv", "-m4755", source, dest])
1700+
.succeeds()
1701+
.stdout_contains("removed")
1702+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1703+
1704+
// Second install with setuid should also copy
1705+
scene
1706+
.ucmd()
1707+
.args(&["-Cv", "-m4755", source, dest])
1708+
.succeeds()
1709+
.stdout_contains("removed")
1710+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1711+
1712+
// Special mode bits - sticky bit
1713+
scene
1714+
.ucmd()
1715+
.args(&["-Cv", "-m1755", source, dest])
1716+
.succeeds()
1717+
.stdout_contains("removed")
1718+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1719+
1720+
// Second install with sticky bit should also copy
1721+
scene
1722+
.ucmd()
1723+
.args(&["-Cv", "-m1755", source, dest])
1724+
.succeeds()
1725+
.stdout_contains("removed")
1726+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1727+
1728+
// Back to normal mode - compare should work again
1729+
scene
1730+
.ucmd()
1731+
.args(&["-Cv", "-m644", source, dest])
1732+
.succeeds()
1733+
.stdout_contains("removed")
1734+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1735+
1736+
// Second install with normal mode should be no-op
1737+
scene
1738+
.ucmd()
1739+
.args(&["-Cv", "-m644", source, dest])
1740+
.succeeds()
1741+
.no_stdout();
1742+
}
1743+
1744+
#[test]
1745+
#[cfg(not(target_os = "openbsd"))]
1746+
fn test_install_compare_group_ownership() {
1747+
let scene = TestScenario::new(util_name!());
1748+
let at = &scene.fixtures;
1749+
1750+
let source = "source_file";
1751+
let dest = "dest_file";
1752+
1753+
at.write(source, "test content");
1754+
1755+
let user_group = std::process::Command::new("id")
1756+
.arg("-nrg")
1757+
.output()
1758+
.map_or_else(
1759+
|_| "users".to_string(),
1760+
|output| String::from_utf8_lossy(&output.stdout).trim().to_string(),
1761+
); // fallback group name
1762+
1763+
// Install with explicit group
1764+
scene
1765+
.ucmd()
1766+
.args(&["-Cv", "-m664", "-g", &user_group, source, dest])
1767+
.succeeds()
1768+
.stdout_contains(format!("'{source}' -> '{dest}'"));
1769+
1770+
// Install without group - this should detect that no copy is needed
1771+
// because the file already has the correct group (user's group)
1772+
scene
1773+
.ucmd()
1774+
.args(&["-Cv", "-m664", source, dest])
1775+
.succeeds()
1776+
.no_stdout(); // Should be no-op if group ownership logic is correct
1777+
}
1778+
1779+
#[test]
1780+
#[cfg(not(target_os = "openbsd"))]
1781+
fn test_install_compare_symlink_handling() {
1782+
let scene = TestScenario::new(util_name!());
1783+
let at = &scene.fixtures;
1784+
1785+
let source = "source_file";
1786+
let symlink_dest = "symlink_dest";
1787+
let target_file = "target_file";
1788+
1789+
at.write(source, "test content");
1790+
at.write(target_file, "test content"); // Same content to test that symlinks are always replaced
1791+
at.symlink_file(target_file, symlink_dest);
1792+
1793+
// Create a symlink as destination pointing to a different file - should always be replaced
1794+
scene
1795+
.ucmd()
1796+
.args(&["-Cv", "-m644", source, symlink_dest])
1797+
.succeeds()
1798+
.stdout_contains("removed")
1799+
.stdout_contains(format!("'{source}' -> '{symlink_dest}'"));
1800+
1801+
// Even if content would be the same, symlink destination should be replaced
1802+
// Now symlink_dest is a regular file, so compare should work normally
1803+
scene
1804+
.ucmd()
1805+
.args(&["-Cv", "-m644", source, symlink_dest])
1806+
.succeeds()
1807+
.no_stdout(); // Now it's a regular file, so compare should work
1808+
}
1809+
16231810
#[test]
16241811
// Matches part of tests/install/basic-1
16251812
fn test_t_exist_dir() {

0 commit comments

Comments
 (0)