@@ -72,16 +72,27 @@ fn install_sh(
72
72
73
73
let prefix = default_path ( & builder. config . prefix , "/usr/local" ) ;
74
74
let sysconfdir = prefix. join ( default_path ( & builder. config . sysconfdir , "/etc" ) ) ;
75
+ let destdir_env = env:: var_os ( "DESTDIR" ) . map ( PathBuf :: from) ;
75
76
76
- // Sanity check for the user write access on prefix and sysconfdir
77
- assert ! (
78
- is_dir_writable_for_user( & prefix) ,
79
- "User doesn't have write access on `install.prefix` path in the `config.toml`." ,
80
- ) ;
81
- assert ! (
82
- is_dir_writable_for_user( & sysconfdir) ,
83
- "User doesn't have write access on `install.sysconfdir` path in `config.toml`."
84
- ) ;
77
+ // Sanity checks on the write access of user.
78
+ //
79
+ // When the `DESTDIR` environment variable is present, there is no point to
80
+ // check write access for `prefix` and `sysconfdir` individually, as they
81
+ // are combined with the path from the `DESTDIR` environment variable. In
82
+ // this case, we only need to check the `DESTDIR` path, disregarding the
83
+ // `prefix` and `sysconfdir` paths.
84
+ if let Some ( destdir) = & destdir_env {
85
+ assert ! ( is_dir_writable_for_user( destdir) , "User doesn't have write access on DESTDIR." ) ;
86
+ } else {
87
+ assert ! (
88
+ is_dir_writable_for_user( & prefix) ,
89
+ "User doesn't have write access on `install.prefix` path in the `config.toml`." ,
90
+ ) ;
91
+ assert ! (
92
+ is_dir_writable_for_user( & sysconfdir) ,
93
+ "User doesn't have write access on `install.sysconfdir` path in `config.toml`."
94
+ ) ;
95
+ }
85
96
86
97
let datadir = prefix. join ( default_path ( & builder. config . datadir , "share" ) ) ;
87
98
let docdir = prefix. join ( default_path ( & builder. config . docdir , "share/doc/rust" ) ) ;
@@ -95,13 +106,13 @@ fn install_sh(
95
106
let mut cmd = Command :: new ( SHELL ) ;
96
107
cmd. current_dir ( & empty_dir)
97
108
. arg ( sanitize_sh ( & tarball. decompressed_output ( ) . join ( "install.sh" ) ) )
98
- . arg ( format ! ( "--prefix={}" , prepare_dir( prefix) ) )
99
- . arg ( format ! ( "--sysconfdir={}" , prepare_dir( sysconfdir) ) )
100
- . arg ( format ! ( "--datadir={}" , prepare_dir( datadir) ) )
101
- . arg ( format ! ( "--docdir={}" , prepare_dir( docdir) ) )
102
- . arg ( format ! ( "--bindir={}" , prepare_dir( bindir) ) )
103
- . arg ( format ! ( "--libdir={}" , prepare_dir( libdir) ) )
104
- . arg ( format ! ( "--mandir={}" , prepare_dir( mandir) ) )
109
+ . arg ( format ! ( "--prefix={}" , prepare_dir( & destdir_env , prefix) ) )
110
+ . arg ( format ! ( "--sysconfdir={}" , prepare_dir( & destdir_env , sysconfdir) ) )
111
+ . arg ( format ! ( "--datadir={}" , prepare_dir( & destdir_env , datadir) ) )
112
+ . arg ( format ! ( "--docdir={}" , prepare_dir( & destdir_env , docdir) ) )
113
+ . arg ( format ! ( "--bindir={}" , prepare_dir( & destdir_env , bindir) ) )
114
+ . arg ( format ! ( "--libdir={}" , prepare_dir( & destdir_env , libdir) ) )
115
+ . arg ( format ! ( "--mandir={}" , prepare_dir( & destdir_env , mandir) ) )
105
116
. arg ( "--disable-ldconfig" ) ;
106
117
builder. run ( & mut cmd) ;
107
118
t ! ( fs:: remove_dir_all( & empty_dir) ) ;
@@ -111,19 +122,16 @@ fn default_path(config: &Option<PathBuf>, default: &str) -> PathBuf {
111
122
config. as_ref ( ) . cloned ( ) . unwrap_or_else ( || PathBuf :: from ( default) )
112
123
}
113
124
114
- fn prepare_dir ( mut path : PathBuf ) -> String {
125
+ fn prepare_dir ( destdir_env : & Option < PathBuf > , mut path : PathBuf ) -> String {
115
126
// The DESTDIR environment variable is a standard way to install software in a subdirectory
116
127
// while keeping the original directory structure, even if the prefix or other directories
117
128
// contain absolute paths.
118
129
//
119
130
// More information on the environment variable is available here:
120
131
// https://www.gnu.org/prep/standards/html_node/DESTDIR.html
121
- if let Some ( destdir) = env:: var_os ( "DESTDIR" ) . map ( PathBuf :: from) {
122
- // Sanity check for the user write access on DESTDIR
123
- assert ! ( is_dir_writable_for_user( & destdir) , "User doesn't have write access on DESTDIR." ) ;
124
-
132
+ if let Some ( destdir) = destdir_env {
125
133
let without_destdir = path. clone ( ) ;
126
- path = destdir;
134
+ path = destdir. clone ( ) ;
127
135
// Custom .join() which ignores disk roots.
128
136
for part in without_destdir. components ( ) {
129
137
if let Component :: Normal ( s) = part {
0 commit comments