You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// we need Arc and Mutex to make this thread-safeuse std::sync::Arc;use std::sync::Mutex;use std::collections::HashMap;structState{map:HashMap<String,String>,init_done:bool,cwd:String}/* add to Cargo.toml:lazy_static = "*"*/
lazy_static::lazy_static! {static ref STATE_ARC: Arc<Mutex<State>> = {// init state. this is called on the first "lock state for this scope"let cwd_str = std::env::current_dir().unwrap().into_os_string().into_string().unwrap(); // pathbuf to stringlet state = State{
map: HashMap::new(),
init_done: false,
cwd: cwd_str
};
let state_arc = Arc::new(Mutex::new(state));
return state_arc;
};
}hook!{unsafefn some_hooked_function(
dir: *const libc::c_char
) -> libc::c_int => my_some_hooked_function {// lock state for this scopeletmut state = STATE_ARC.lock().unwrap();
// use state ...if !state.init_done {
println!("preload init. cwd = {}", state.cwd);
state.init_done = true;
}let map_from = "foo";
let map_to = "bar";
state.map.insert(map_from.to_owned(), map_to.to_owned()); // must copy valuesfor map_from in state.map.keys(){let map_to = state.map.get(map_from);
println!("preload map {} -> {}", map_from, map_to);
}}}hook!{unsafefn chdir(
dir: *const libc::c_char
) -> libc::c_int => my_chdir {let retval = real!(chdir)(dir);
let dir_str = str_of_chars(dir);
if retval == 0{// success// lock state for this scopeletmut state = STATE_ARC.lock().unwrap();
if dir_str.starts_with("/"){
state.cwd = dir_str.to_owned();
}
else {// get absolute path// TODO better: resolve ../letmut dir_abs = state.cwd.to_owned();
dir_abs.push_str("/");
dir_abs.push_str(dir_str);
state.cwd = dir_abs;
}
println!("preload chdir {}", state.cwd);
}return retval;
}}
The text was updated successfully, but these errors were encountered:
could be useful. add to examples?
The text was updated successfully, but these errors were encountered: