From 911587692cd22071c8f8fc8361d90a17a71806ef Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 23 Dec 2022 22:13:53 -0300 Subject: [PATCH] feat(core): metadata for runtime fs scope path additions, closes #5875 --- .changes/fs-scope-metadata.md | 5 +++ core/tauri/src/scope/fs.rs | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .changes/fs-scope-metadata.md diff --git a/.changes/fs-scope-metadata.md b/.changes/fs-scope-metadata.md new file mode 100644 index 000000000..b6b8ef119 --- /dev/null +++ b/.changes/fs-scope-metadata.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Add metadata getters for runtime allowed and forbidden paths. diff --git a/core/tauri/src/scope/fs.rs b/core/tauri/src/scope/fs.rs index 8979eb8f1..efd0b7f5c 100644 --- a/core/tauri/src/scope/fs.rs +++ b/core/tauri/src/scope/fs.rs @@ -29,12 +29,33 @@ pub enum Event { type EventListener = Box; +/// Metadata for an allowed or forbidden path. +#[derive(Clone)] +pub struct PathMetadata { + is_dir: bool, + recursive: bool, +} + +impl PathMetadata { + /// Whether the path represents a directory or not. + pub fn is_dir(&self) -> bool { + self.is_dir + } + + /// Whether the path represents a recursive directory or not. + pub fn recursive(&self) -> bool { + self.recursive + } +} + /// Scope for filesystem access. #[derive(Clone)] pub struct Scope { allowed_patterns: Arc>>, forbidden_patterns: Arc>>, event_listeners: Arc>>, + allowed_paths_metadata: Arc>>, + forbidden_paths_metadata: Arc>>, } impl fmt::Debug for Scope { @@ -110,6 +131,8 @@ impl Scope { allowed_patterns: Arc::new(Mutex::new(allowed_patterns)), forbidden_patterns: Arc::new(Mutex::new(forbidden_patterns)), event_listeners: Default::default(), + allowed_paths_metadata: Default::default(), + forbidden_paths_metadata: Default::default(), }) } @@ -130,6 +153,26 @@ impl Scope { id } + /// Gets the metadata for the given allowed path. + pub fn allowed_path_metadata(&self, path: &Path) -> Option { + self + .allowed_paths_metadata + .lock() + .unwrap() + .get(path) + .cloned() + } + + /// Gets the metadata for the given fobidden path. + pub fn forbidden_path_metadata(&self, path: &Path) -> Option { + self + .forbidden_paths_metadata + .lock() + .unwrap() + .get(path) + .cloned() + } + fn trigger(&self, event: Event) { let listeners = self.event_listeners.lock().unwrap(); let handlers = listeners.values(); @@ -154,6 +197,13 @@ impl Scope { escaped_pattern_with(p, if recursive { "**" } else { "*" }) })?; } + self.allowed_paths_metadata.lock().unwrap().insert( + path.to_path_buf(), + PathMetadata { + is_dir: true, + recursive, + }, + ); self.trigger(Event::PathAllowed(path.to_path_buf())); Ok(()) } @@ -168,6 +218,13 @@ impl Scope { path, escaped_pattern, )?; + self.allowed_paths_metadata.lock().unwrap().insert( + path.to_path_buf(), + PathMetadata { + is_dir: false, + recursive: false, + }, + ); self.trigger(Event::PathAllowed(path.to_path_buf())); Ok(()) } @@ -187,6 +244,13 @@ impl Scope { escaped_pattern_with(p, if recursive { "**" } else { "*" }) })?; } + self.forbidden_paths_metadata.lock().unwrap().insert( + path.to_path_buf(), + PathMetadata { + is_dir: true, + recursive, + }, + ); self.trigger(Event::PathForbidden(path.to_path_buf())); Ok(()) } @@ -201,6 +265,13 @@ impl Scope { path, escaped_pattern, )?; + self.forbidden_paths_metadata.lock().unwrap().insert( + path.to_path_buf(), + PathMetadata { + is_dir: false, + recursive: false, + }, + ); self.trigger(Event::PathForbidden(path.to_path_buf())); Ok(()) }