Skip to content

Commit bb6c66b

Browse files
committed
Auto merge of rust-lang#116127 - onur-ozkan:sanity-checks-on-install, r=Mark-Simulacrum
add sanity checks for user write access on `x install` Resolves rust-lang#113580
2 parents 05c6221 + 350ead8 commit bb6c66b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/bootstrap/install.rs

+31
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ fn sanitize_sh(path: &Path) -> String {
4545
}
4646
}
4747

48+
fn is_dir_writable_for_user(dir: &PathBuf) -> bool {
49+
let tmp_file = dir.join(".tmp");
50+
match fs::File::create(&tmp_file) {
51+
Ok(_) => {
52+
fs::remove_file(tmp_file).unwrap();
53+
true
54+
}
55+
Err(e) => {
56+
if e.kind() == std::io::ErrorKind::PermissionDenied {
57+
false
58+
} else {
59+
panic!("Failed the write access check for the current user. {}", e);
60+
}
61+
}
62+
}
63+
}
64+
4865
fn install_sh(
4966
builder: &Builder<'_>,
5067
package: &str,
@@ -56,6 +73,17 @@ fn install_sh(
5673

5774
let prefix = default_path(&builder.config.prefix, "/usr/local");
5875
let sysconfdir = prefix.join(default_path(&builder.config.sysconfdir, "/etc"));
76+
77+
// Sanity check for the user write access on prefix and sysconfdir
78+
assert!(
79+
is_dir_writable_for_user(&prefix),
80+
"User doesn't have write access on `install.prefix` path in the `config.toml`.",
81+
);
82+
assert!(
83+
is_dir_writable_for_user(&sysconfdir),
84+
"User doesn't have write access on `install.sysconfdir` path in `config.toml`."
85+
);
86+
5987
let datadir = prefix.join(default_path(&builder.config.datadir, "share"));
6088
let docdir = prefix.join(default_path(&builder.config.docdir, "share/doc/rust"));
6189
let mandir = prefix.join(default_path(&builder.config.mandir, "share/man"));
@@ -92,6 +120,9 @@ fn prepare_dir(mut path: PathBuf) -> String {
92120
// More information on the environment variable is available here:
93121
// https://www.gnu.org/prep/standards/html_node/DESTDIR.html
94122
if let Some(destdir) = env::var_os("DESTDIR").map(PathBuf::from) {
123+
// Sanity check for the user write access on DESTDIR
124+
assert!(is_dir_writable_for_user(&destdir), "User doesn't have write access on DESTDIR.");
125+
95126
let without_destdir = path.clone();
96127
path = destdir;
97128
// Custom .join() which ignores disk roots.

0 commit comments

Comments
 (0)