@@ -210,70 +210,16 @@ impl PackageInstaller {
210210
211211 /// Run a hook command with environment variables set.
212212 fn run_hook ( & self , hook_name : & str , command : & str ) -> SoarResult < ( ) > {
213- use crate :: sandbox;
214-
215- debug ! ( "running {} hook: {}" , hook_name, command) ;
216-
217- let bin_dir = get_config ( ) . get_bin_path ( ) ?;
213+ use super :: hooks:: { run_hook, HookEnv } ;
218214
219- let env_vars: Vec < ( & str , & str ) > = vec ! [
220- ( "INSTALL_DIR" , self . install_dir. to_str( ) . unwrap_or( "" ) ) ,
221- ( "BIN_DIR" , bin_dir. to_str( ) . unwrap_or( "" ) ) ,
222- ( "PKG_NAME" , & self . package. pkg_name) ,
223- ( "PKG_ID" , & self . package. pkg_id) ,
224- ( "PKG_VERSION" , & self . package. version) ,
225- ] ;
226-
227- let status = if sandbox:: is_landlock_supported ( ) {
228- debug ! ( "running {} hook with Landlock sandbox" , hook_name) ;
229- let mut cmd = sandbox:: SandboxedCommand :: new ( command)
230- . working_dir ( & self . install_dir )
231- . read_path ( & bin_dir)
232- . envs ( env_vars) ;
233-
234- if let Some ( s) = & self . sandbox {
235- let config = sandbox:: SandboxConfig :: new ( ) . with_network ( if s. network {
236- sandbox:: NetworkConfig :: allow_all ( )
237- } else {
238- sandbox:: NetworkConfig :: default ( )
239- } ) ;
240- cmd = cmd. config ( config) ;
241- for path in & s. fs_read {
242- cmd = cmd. read_path ( path) ;
243- }
244- for path in & s. fs_write {
245- cmd = cmd. write_path ( path) ;
246- }
247- }
248- cmd. run ( ) ?
249- } else {
250- use std:: process:: Command ;
251- warn ! (
252- "Landlock not supported, running {} hook without sandbox" ,
253- hook_name
254- ) ;
255- Command :: new ( "sh" )
256- . arg ( "-c" )
257- . arg ( command)
258- . env ( "INSTALL_DIR" , & self . install_dir )
259- . env ( "BIN_DIR" , & bin_dir)
260- . env ( "PKG_NAME" , & self . package . pkg_name )
261- . env ( "PKG_ID" , & self . package . pkg_id )
262- . env ( "PKG_VERSION" , & self . package . version )
263- . current_dir ( & self . install_dir )
264- . status ( )
265- . with_context ( || format ! ( "executing {} hook" , hook_name) ) ?
215+ let env = HookEnv {
216+ install_dir : & self . install_dir ,
217+ pkg_name : & self . package . pkg_name ,
218+ pkg_id : & self . package . pkg_id ,
219+ pkg_version : & self . package . version ,
266220 } ;
267221
268- if !status. success ( ) {
269- return Err ( SoarError :: Custom ( format ! (
270- "{} hook failed with exit code: {}" ,
271- hook_name,
272- status. code( ) . unwrap_or( -1 )
273- ) ) ) ;
274- }
275-
276- Ok ( ( ) )
222+ run_hook ( hook_name, command, & env, self . sandbox . as_ref ( ) )
277223 }
278224
279225 /// Run post_download hook if configured.
0 commit comments