Skip to content

Commit

Permalink
fix(plugin): Set preopen directories properly
Browse files Browse the repository at this point in the history
Every plugin will have following two directories for its use:

`./`: Plugin's own data should be saved here, every plugin will have its own directory.
`/global/`: All plugins have access to this directory, Some shared data could be saved here.

Signed-off-by: Tw <tw19881113@gmail.com>
  • Loading branch information
tw4452852 committed Sep 14, 2021
1 parent 6459924 commit 5e47644
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions zellij-server/src/wasm_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ pub(crate) struct PluginEnv {
pub subscriptions: Arc<Mutex<HashSet<EventType>>>,
// FIXME: Once permission system is ready, this could be removed
pub _allow_exec_host_cmd: bool,
plugin_own_data_dir: PathBuf,
}

// Thread main --------------------------------------------------------------------------------------------------------
pub(crate) fn wasm_thread_main(bus: Bus<PluginInstruction>, store: Store, data_dir: PathBuf) {
info!("Wasm main thread starts");
let mut plugin_id = 0;
let mut plugin_map = HashMap::new();
let plugin_dir = data_dir.join("plugins/");
let plugin_global_data_dir = plugin_dir.join("data");
fs::create_dir_all(plugin_global_data_dir.as_path()).unwrap();

loop {
let (event, mut err_ctx) = bus.recv().expect("failed to receive event on channel");
err_ctx.add_call(ContextType::Plugin((&event).into()));
match event {
PluginInstruction::Load(pid_tx, path, tab_index, _allow_exec_host_cmd) => {
let plugin_dir = data_dir.join("plugins/");
let wasm_bytes = fs::read(&path)
.or_else(|_| fs::read(&path.with_extension("wasm")))
.or_else(|_| fs::read(&plugin_dir.join(&path).with_extension("wasm")))
Expand All @@ -83,15 +87,14 @@ pub(crate) fn wasm_thread_main(bus: Bus<PluginInstruction>, store: Store, data_d
path.as_path().file_name().unwrap().to_str().unwrap(),
plugin_id,
);

let plugin_own_data_dir = plugin_global_data_dir.join(plugin_id.to_string());
fs::create_dir_all(plugin_own_data_dir.as_path()).unwrap();
let mut wasi_env = WasiState::new("Zellij")
.env("CLICOLOR_FORCE", "1")
.preopen(|p| {
p.directory(".") // FIXME: Change this to a more meaningful dir
.alias(".")
.read(true)
.write(true)
.create(true)
})
.map_dir(".", plugin_own_data_dir.as_path())
.unwrap()
.map_dir("/global", plugin_global_data_dir.as_path())
.unwrap()
.stdin(Box::new(input))
.stdout(Box::new(output))
Expand All @@ -112,6 +115,7 @@ pub(crate) fn wasm_thread_main(bus: Bus<PluginInstruction>, store: Store, data_d
wasi_env,
subscriptions: Arc::new(Mutex::new(HashSet::new())),
_allow_exec_host_cmd,
plugin_own_data_dir,
};

let zellij = zellij_exports(&store, &plugin_env);
Expand Down Expand Up @@ -154,10 +158,17 @@ pub(crate) fn wasm_thread_main(bus: Bus<PluginInstruction>, store: Store, data_d
buf_tx.send(wasi_read_string(&plugin_env.wasi_env)).unwrap();
}
}
PluginInstruction::Unload(pid) => drop(plugin_map.remove(&pid)),
PluginInstruction::Unload(pid) => {
info!("Bye from plugin {}", &pid);
let (_, plugin_env) = plugin_map.get(&pid).unwrap();
fs::remove_dir_all(plugin_env.plugin_own_data_dir.as_path()).unwrap();
drop(plugin_map.remove(&pid));
}
PluginInstruction::Exit => break,
}
}
info!("Bye from wasm main thread");
fs::remove_dir_all(plugin_global_data_dir.as_path()).unwrap();
}

// Plugin API ---------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 5e47644

Please sign in to comment.