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