Skip to content

Commit

Permalink
refactor: update default cache function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeheonji committed Jul 31, 2023
1 parent 29a2fad commit 171a5e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion zellij-server/src/plugins/wasm_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ impl WasmBridge {
.plugin_env
.set_permissions(HashSet::from_iter(permissions.clone()));

let mut granted_permission = GrantedPermission::from_default().unwrap_or_default();
let mut granted_permission = GrantedPermission::from_cache_or_default();
granted_permission.insert(
running_plugin.plugin_env.plugin.location.to_string(),
permissions,
Expand Down
25 changes: 12 additions & 13 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,18 @@ fn host_set_selectable(env: &ForeignFunctionEnv, selectable: i32) {
fn host_request_permission(env: &ForeignFunctionEnv) {
wasi_read_object::<Vec<PermissionType>>(&env.plugin_env.wasi_env)
.and_then(|permissions| {
if let Ok(granted_permission) = GrantedPermission::from_default() {
if let Some(p) = granted_permission.get(&env.plugin_env.plugin.location.to_string())
{
if p.to_vec() == permissions {
return env.plugin_env.senders.send_to_plugin(
PluginInstruction::PermissionRequestResult(
env.plugin_env.plugin_id,
Some(env.plugin_env.client_id),
permissions.to_vec(),
zellij_utils::data::PermissionStatus::Granted,
),
);
}
if let Some(p) = GrantedPermission::from_cache_or_default()
.get(&env.plugin_env.plugin.location.to_string())
{
if p.to_vec() == permissions {
return env.plugin_env.senders.send_to_plugin(
PluginInstruction::PermissionRequestResult(
env.plugin_env.plugin_id,
Some(env.plugin_env.client_id),
permissions.to_vec(),
zellij_utils::data::PermissionStatus::Granted,
),
);
}
}

Expand Down
11 changes: 5 additions & 6 deletions zellij-utils/src/input/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use crate::{
consts::{ZELLIJ_CACHE_DIR, ZELLIJ_PLUGIN_PERMISSIONS_FILE},
data::PermissionType,
input::config::ConfigError,
};

#[derive(Default, Debug)]
Expand All @@ -25,12 +24,12 @@ impl GrantedPermission {
self.0.iter()
}

pub fn from_default() -> Result<Self, ConfigError> {
pub fn from_cache_or_default() -> Self {
let default_permission = ZELLIJ_CACHE_DIR.join(ZELLIJ_PLUGIN_PERMISSIONS_FILE);

let raw_string = fs::read_to_string(&default_permission)
.map_err(|e| ConfigError::IoPath(e, default_permission.into()))?;

GrantedPermission::from_string(raw_string)
match fs::read_to_string(&default_permission) {
Ok(s) => GrantedPermission::from_string(s).unwrap_or_default(),
Err(_) => GrantedPermission::default(),
}
}
}

0 comments on commit 171a5e8

Please sign in to comment.