@@ -121,6 +121,9 @@ struct Install {
121121 /// Whether to bypass the install prompt.
122122 #[ clap( long) ]
123123 yes : bool ,
124+ /// Whether to bypass the root check
125+ #[ clap( long) ]
126+ bypass_root_check : bool ,
124127}
125128
126129#[ derive( Parser ) ]
@@ -140,6 +143,9 @@ struct Remove {
140143 /// Whether to bypass the remove prompt.
141144 #[ clap( long) ]
142145 yes : bool ,
146+ /// Whether to bypass the root check
147+ #[ clap( long) ]
148+ bypass_root_check : bool ,
143149}
144150
145151#[ cfg( not( windows) ) ]
@@ -193,6 +199,11 @@ impl Install {
193199 self . no_default_features ,
194200 ) ?;
195201
202+ if !self . bypass_root_check {
203+ #[ cfg( unix) ]
204+ sudo:: escalate_if_needed ( ) . expect ( "failed to escalate root privileges." ) ;
205+ }
206+
196207 let ( mut ext_dir, mut php_ini) = if let Some ( install_dir) = self . install_dir {
197208 ( install_dir, None )
198209 } else {
@@ -221,16 +232,9 @@ impl Install {
221232 ext_dir. push ( ext_name) ;
222233 }
223234
224- // We copying of file fails, escalate the privilege and try again.
225- if let Err ( _) = std:: fs:: copy ( & ext_path, & ext_dir) {
226- // failed to copy. escalate the privileges and try again.
227- #[ cfg( unix) ]
228- let _ = sudo:: escalate_if_needed ( ) . ok ( ) ;
229-
230- std:: fs:: copy ( & ext_path, & ext_dir) . with_context ( || {
231- "Failed to copy extension from target directory to extension directory"
232- } ) ?;
233- }
235+ std:: fs:: copy ( & ext_path, & ext_dir) . with_context ( || {
236+ "Failed to copy extension from target directory to extension directory"
237+ } ) ?;
234238
235239 if let Some ( php_ini) = php_ini {
236240 let mut file = OpenOptions :: new ( )
@@ -323,6 +327,11 @@ impl Remove {
323327 pub fn handle ( self ) -> CrateResult {
324328 use std:: env:: consts;
325329
330+ if !self . bypass_root_check {
331+ #[ cfg( unix) ]
332+ sudo:: escalate_if_needed ( ) . expect ( "failed to escalate root privileges." ) ;
333+ }
334+
326335 let artifact = find_ext ( self . manifest . as_ref ( ) ) ?;
327336
328337 let ( mut ext_path, mut php_ini) = if let Some ( install_dir) = self . install_dir {
0 commit comments