@@ -72,16 +72,27 @@ fn install_sh(
7272
7373 let prefix = default_path ( & builder. config . prefix , "/usr/local" ) ;
7474 let sysconfdir = prefix. join ( default_path ( & builder. config . sysconfdir , "/etc" ) ) ;
75+ let destdir_env = env:: var_os ( "DESTDIR" ) . map ( PathBuf :: from) ;
7576
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+ }
8596
8697 let datadir = prefix. join ( default_path ( & builder. config . datadir , "share" ) ) ;
8798 let docdir = prefix. join ( default_path ( & builder. config . docdir , "share/doc/rust" ) ) ;
@@ -95,13 +106,13 @@ fn install_sh(
95106 let mut cmd = Command :: new ( SHELL ) ;
96107 cmd. current_dir ( & empty_dir)
97108 . 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) ) )
105116 . arg ( "--disable-ldconfig" ) ;
106117 builder. run ( & mut cmd) ;
107118 t ! ( fs:: remove_dir_all( & empty_dir) ) ;
@@ -111,19 +122,16 @@ fn default_path(config: &Option<PathBuf>, default: &str) -> PathBuf {
111122 config. as_ref ( ) . cloned ( ) . unwrap_or_else ( || PathBuf :: from ( default) )
112123}
113124
114- fn prepare_dir ( mut path : PathBuf ) -> String {
125+ fn prepare_dir ( destdir_env : & Option < PathBuf > , mut path : PathBuf ) -> String {
115126 // The DESTDIR environment variable is a standard way to install software in a subdirectory
116127 // while keeping the original directory structure, even if the prefix or other directories
117128 // contain absolute paths.
118129 //
119130 // More information on the environment variable is available here:
120131 // 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 {
125133 let without_destdir = path. clone ( ) ;
126- path = destdir;
134+ path = destdir. clone ( ) ;
127135 // Custom .join() which ignores disk roots.
128136 for part in without_destdir. components ( ) {
129137 if let Component :: Normal ( s) = part {
0 commit comments