From 9be0f3057d68e2e0d708910b3d383b6a5db44343 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Tue, 25 Apr 2023 12:40:34 +0200 Subject: [PATCH] manually re-save state once --- plugins/persisted-scope/src/lib.rs | 54 ++++++++++++++++++------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/plugins/persisted-scope/src/lib.rs b/plugins/persisted-scope/src/lib.rs index e50daeb2..114ed970 100644 --- a/plugins/persisted-scope/src/lib.rs +++ b/plugins/persisted-scope/src/lib.rs @@ -6,12 +6,13 @@ use aho_corasick::AhoCorasick; use serde::{Deserialize, Serialize}; use tauri::{ plugin::{Builder, TauriPlugin}, - FsScopeEvent, Manager, Runtime, + AppHandle, FsScopeEvent, Manager, Runtime, }; use std::{ fs::{create_dir_all, File}, io::Write, + path::Path, }; const SCOPE_STATE_FILENAME: &str = ".persisted-scope"; @@ -57,6 +58,31 @@ fn fix_pattern(ac: &AhoCorasick, s: &str) -> String { s } +fn save_scopes(app: &AppHandle, app_dir: &Path, scope_state_path: &Path) { + let fs_scope = app.fs_scope(); + + let scope = Scope { + allowed_paths: fs_scope + .allowed_patterns() + .into_iter() + .map(|p| dbg!(p.to_string())) + .collect(), + forbidden_patterns: fs_scope + .forbidden_patterns() + .into_iter() + .map(|p| p.to_string()) + .collect(), + }; + + let _ = create_dir_all(app_dir) + .and_then(|_| File::create(scope_state_path)) + .map_err(Error::Io) + .and_then(|mut f| { + f.write_all(&bincode::serialize(&scope).map_err(Error::from)?) + .map_err(Into::into) + }); +} + pub fn init() -> TauriPlugin { Builder::new("persisted-scope") .setup(|app| { @@ -96,31 +122,15 @@ pub fn init() -> TauriPlugin { #[cfg(feature = "protocol-asset")] let _ = asset_protocol_scope.forbid_file(&forbidden); } + + // Manually save the fixed scopes to disk once. + // This is needed to fix broken .peristed-scope files in case the app doesn't update the scope itself. + save_scopes(&app, &app_dir, &scope_state_path); } fs_scope.listen(move |event| { - let fs_scope = app.fs_scope(); if let FsScopeEvent::PathAllowed(_) = event { - let scope = Scope { - allowed_paths: fs_scope - .allowed_patterns() - .into_iter() - .map(|p| p.to_string()) - .collect(), - forbidden_patterns: fs_scope - .forbidden_patterns() - .into_iter() - .map(|p| p.to_string()) - .collect(), - }; - - let _ = create_dir_all(&app_dir) - .and_then(|_| File::create(&scope_state_path)) - .map_err(Error::Io) - .and_then(|mut f| { - f.write_all(&bincode::serialize(&scope).map_err(Error::from)?) - .map_err(Into::into) - }); + save_scopes(&app, &app_dir, &scope_state_path); } }); }