Skip to content

Commit 0f2e408

Browse files
committed
just breaking stuff, don't mind me
1 parent f323eb8 commit 0f2e408

File tree

8 files changed

+58
-260
lines changed

8 files changed

+58
-260
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
fs: patch
2+
fs: minor
3+
persisted-scope: minor
34
---
45

5-
Paths given to the `Scope` methods are now correctly escaped, preventing issues with paths containing `[]`.
6+
**Breaking Change:** Replaced the custom `tauri_plugin_fs::Scope` struct with `tauri::fs::Scope`.

plugins/dialog/src/commands.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub(crate) async fn open<R: Runtime>(
143143
for folder in folders {
144144
if let Ok(path) = folder.clone().into_path() {
145145
if let Some(s) = window.try_fs_scope() {
146-
s.allow_directory(&path, options.recursive);
146+
let _ = s.allow_directory(&path, options.recursive);
147147
}
148148
tauri_scope.allow_directory(&path, options.directory)?;
149149
}
@@ -157,7 +157,7 @@ pub(crate) async fn open<R: Runtime>(
157157
if let Some(folder) = &folder {
158158
if let Ok(path) = folder.clone().into_path() {
159159
if let Some(s) = window.try_fs_scope() {
160-
s.allow_directory(&path, options.recursive);
160+
let _ = s.allow_directory(&path, options.recursive);
161161
}
162162
tauri_scope.allow_directory(&path, options.directory)?;
163163
}
@@ -175,7 +175,7 @@ pub(crate) async fn open<R: Runtime>(
175175
for file in files {
176176
if let Ok(path) = file.clone().into_path() {
177177
if let Some(s) = window.try_fs_scope() {
178-
s.allow_file(&path);
178+
let _ = s.allow_file(&path);
179179
}
180180

181181
tauri_scope.allow_file(&path)?;
@@ -190,7 +190,7 @@ pub(crate) async fn open<R: Runtime>(
190190
if let Some(file) = &file {
191191
if let Ok(path) = file.clone().into_path() {
192192
if let Some(s) = window.try_fs_scope() {
193-
s.allow_file(&path);
193+
let _ = s.allow_file(&path);
194194
}
195195
tauri_scope.allow_file(&path)?;
196196
}
@@ -232,7 +232,7 @@ pub(crate) async fn save<R: Runtime>(
232232
if let Some(p) = &path {
233233
if let Ok(path) = p.clone().into_path() {
234234
if let Some(s) = window.try_fs_scope() {
235-
s.allow_file(&path);
235+
let _ = s.allow_file(&path);
236236
}
237237
tauri_scope.allow_file(&path)?;
238238
}

plugins/fs/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
path::{Path, PathBuf},
88
};
99

10-
#[path = "src/entryraw.rs"]
10+
#[path = "src/scope.rs"]
1111
#[allow(dead_code)]
1212
mod scope;
1313

plugins/fs/src/commands.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::{
2222
time::{SystemTime, UNIX_EPOCH},
2323
};
2424

25-
use crate::{scope::Entry, Error, FsExt, SafeFilePath};
25+
use crate::{scope::Entry, Error, SafeFilePath};
2626

2727
#[derive(Debug, thiserror::Error)]
2828
pub enum CommandError {
@@ -999,6 +999,8 @@ pub fn resolve_path<R: Runtime>(
999999
path
10001000
};
10011001

1002+
let fs_scope = webview.state::<crate::Scope>();
1003+
10021004
let scope = tauri::scope::fs::Scope::new(
10031005
webview,
10041006
&FsScope::Scope {
@@ -1014,31 +1016,19 @@ pub fn resolve_path<R: Runtime>(
10141016
.filter_map(|e| e.path.clone())
10151017
.chain(command_scope.denies().iter().filter_map(|e| e.path.clone()))
10161018
.collect(),
1017-
require_literal_leading_dot: webview.fs_scope().require_literal_leading_dot,
1019+
require_literal_leading_dot: fs_scope.require_literal_leading_dot,
10181020
},
10191021
)?;
10201022

1021-
let fs_scope = webview.fs_scope();
1022-
10231023
let require_literal_leading_dot = fs_scope.require_literal_leading_dot.unwrap_or(cfg!(unix));
10241024

1025-
if fs_scope
1026-
.scope
1027-
.as_ref()
1028-
.map(|s| is_forbidden(s, &path, require_literal_leading_dot))
1029-
.unwrap_or(false)
1025+
if is_forbidden(&fs_scope.scope, &path, require_literal_leading_dot)
10301026
|| is_forbidden(&scope, &path, require_literal_leading_dot)
10311027
{
10321028
return Err(CommandError::Plugin(Error::PathForbidden(path)));
10331029
}
10341030

1035-
if fs_scope
1036-
.scope
1037-
.as_ref()
1038-
.map(|s| s.is_allowed(&path))
1039-
.unwrap_or(false)
1040-
|| scope.is_allowed(&path)
1041-
{
1031+
if fs_scope.scope.is_allowed(&path) || scope.is_allowed(&path) {
10421032
Ok(path)
10431033
} else {
10441034
Err(CommandError::Plugin(Error::PathForbidden(path)))

plugins/fs/src/entryraw.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

plugins/fs/src/lib.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ mod commands;
2323
mod config;
2424
#[cfg(not(target_os = "android"))]
2525
mod desktop;
26-
mod entryraw;
2726
mod error;
2827
mod file_path;
2928
#[cfg(target_os = "android")]
@@ -40,7 +39,6 @@ pub use desktop::Fs;
4039
pub use mobile::Fs;
4140

4241
pub use error::Error;
43-
pub use scope::{Event as ScopeEvent, Scope};
4442

4543
pub use file_path::FilePath;
4644
pub use file_path::SafeFilePath;
@@ -353,8 +351,8 @@ impl ScopeObject for scope::Entry {
353351
raw: Value,
354352
) -> std::result::Result<Self, Self::Error> {
355353
let path = serde_json::from_value(raw.into()).map(|raw| match raw {
356-
entryraw::EntryRaw::Value(path) => path,
357-
entryraw::EntryRaw::Object { path } => path,
354+
scope::EntryRaw::Value(path) => path,
355+
scope::EntryRaw::Object { path } => path,
358356
})?;
359357

360358
match app.path().parse(path) {
@@ -366,21 +364,26 @@ impl ScopeObject for scope::Entry {
366364
}
367365
}
368366

367+
pub(crate) struct Scope {
368+
pub(crate) scope: tauri::fs::Scope,
369+
pub(crate) require_literal_leading_dot: Option<bool>,
370+
}
371+
369372
pub trait FsExt<R: Runtime> {
370-
fn fs_scope(&self) -> &Scope;
371-
fn try_fs_scope(&self) -> Option<&Scope>;
373+
fn fs_scope(&self) -> tauri::fs::Scope;
374+
fn try_fs_scope(&self) -> Option<tauri::fs::Scope>;
372375

373376
/// Cross platform file system APIs that also support manipulating Android files.
374377
fn fs(&self) -> &Fs<R>;
375378
}
376379

377380
impl<R: Runtime, T: Manager<R>> FsExt<R> for T {
378-
fn fs_scope(&self) -> &Scope {
379-
self.state::<Scope>().inner()
381+
fn fs_scope(&self) -> tauri::fs::Scope {
382+
self.state::<Scope>().scope.clone()
380383
}
381384

382-
fn try_fs_scope(&self) -> Option<&Scope> {
383-
self.try_state::<Scope>().map(|s| s.inner())
385+
fn try_fs_scope(&self) -> Option<tauri::fs::Scope> {
386+
self.try_state::<Scope>().map(|s| s.scope.clone())
384387
}
385388

386389
fn fs(&self) -> &Fs<R> {
@@ -425,7 +428,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
425428
.config()
426429
.as_ref()
427430
.and_then(|c| c.require_literal_leading_dot),
428-
scope: Some(tauri::fs::Scope::new(app, &FsScope::default())?),
431+
scope: tauri::fs::Scope::new(app, &FsScope::default())?,
429432
};
430433

431434
#[cfg(target_os = "android")]
@@ -449,9 +452,9 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
449452
let scope = app.fs_scope();
450453
for path in paths {
451454
if path.is_file() {
452-
scope.allow_file(path);
455+
let _ = scope.allow_file(path);
453456
} else {
454-
scope.allow_directory(path, true);
457+
let _ = scope.allow_directory(path, true);
455458
}
456459
}
457460
}

plugins/fs/src/scope.rs

Lines changed: 8 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2,126 +2,18 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::{
6-
collections::HashSet,
7-
path::{Path, PathBuf},
8-
};
5+
use std::path::PathBuf;
6+
7+
use serde::Deserialize;
98

109
#[derive(Debug)]
1110
pub struct Entry {
1211
pub path: Option<PathBuf>,
1312
}
1413

15-
pub type EventId = u32;
16-
17-
/// Scope change event.
18-
#[derive(Debug, Clone)]
19-
pub enum Event {
20-
/// A path has been allowed.
21-
PathAllowed(PathBuf),
22-
/// A path has been forbidden.
23-
PathForbidden(PathBuf),
24-
}
25-
26-
#[derive(Default)]
27-
pub struct Scope {
28-
// TODO: Remove Option in v2, just used to keep Default
29-
pub(crate) scope: Option<tauri::fs::Scope>,
30-
pub(crate) require_literal_leading_dot: Option<bool>,
31-
}
32-
33-
impl Scope {
34-
/// Extend the allowed patterns with the given directory.
35-
///
36-
/// After this function has been called, the frontend will be able to use the Tauri API to read
37-
/// the directory and all of its files. If `recursive` is `true`, subdirectories will be accessible too.
38-
// TODO: Return Result
39-
pub fn allow_directory<P: AsRef<Path>>(&self, path: P, recursive: bool) {
40-
if let Some(scope) = &self.scope {
41-
let _ = scope.allow_directory(path, recursive);
42-
}
43-
}
44-
45-
/// Extend the allowed patterns with the given file path.
46-
///
47-
/// After this function has been called, the frontend will be able to use the Tauri API to read the contents of this file.
48-
// TODO: Return Result
49-
pub fn allow_file<P: AsRef<Path>>(&self, path: P) {
50-
if let Some(scope) = &self.scope {
51-
let _ = scope.allow_file(path);
52-
}
53-
}
54-
55-
/// Set the given directory path to be forbidden by this scope.
56-
///
57-
/// **Note:** this takes precedence over allowed paths, so its access gets denied **always**.
58-
// TODO: Return Result
59-
pub fn forbid_directory<P: AsRef<Path>>(&self, path: P, recursive: bool) {
60-
if let Some(scope) = &self.scope {
61-
let _ = scope.forbid_directory(path, recursive);
62-
}
63-
}
64-
65-
/// Set the given file path to be forbidden by this scope.
66-
///
67-
/// **Note:** this takes precedence over allowed paths, so its access gets denied **always**.
68-
// TODO: Return Result
69-
pub fn forbid_file<P: AsRef<Path>>(&self, path: P) {
70-
if let Some(scope) = &self.scope {
71-
let _ = scope.forbid_file(path);
72-
}
73-
}
74-
75-
/// List of allowed paths.
76-
#[deprecated(since = "2.1.0", note = "use `allowed_patterns` instead")]
77-
pub fn allowed(&self) -> Vec<PathBuf> {
78-
self.scope
79-
.as_ref()
80-
.map(|s| s.allowed_patterns().clone())
81-
.unwrap_or_default()
82-
.iter()
83-
.map(|p| PathBuf::from(p.as_str()))
84-
.collect()
85-
}
86-
87-
/// List of allowed patterns. Note that this does not include paths defined in capabilites.
88-
pub fn allowed_patterns(&self) -> HashSet<tauri::fs::Pattern> {
89-
self.scope
90-
.as_ref()
91-
.map(|s| s.allowed_patterns().clone())
92-
.unwrap_or_default()
93-
}
94-
95-
/// List of forbidden paths.
96-
#[deprecated(since = "2.1.0", note = "use `forbidden_patterns` instead")]
97-
pub fn forbidden(&self) -> Vec<PathBuf> {
98-
self.scope
99-
.as_ref()
100-
.map(|s| s.forbidden_patterns().clone())
101-
.unwrap_or_default()
102-
.iter()
103-
.map(|p| PathBuf::from(p.as_str()))
104-
.collect()
105-
}
106-
107-
/// List of forbidden patterns. Note that this does not include paths defined in capabilites.
108-
pub fn forbidden_patterns(&self) -> HashSet<tauri::fs::Pattern> {
109-
self.scope
110-
.as_ref()
111-
.map(|s| s.forbidden_patterns())
112-
.unwrap_or_default()
113-
}
114-
115-
/// Listen to an event on this scope.
116-
/// Silently fails and returns `0` until v3 if `Scope` was constructed manually instead of getting it via `app.fs_scope()`.
117-
pub fn listen<F: Fn(&Event) + Send + 'static>(&self, f: F) -> EventId {
118-
if let Some(scope) = &self.scope {
119-
scope.listen(move |e| match e {
120-
tauri::fs::Event::PathAllowed(p) => f(&Event::PathAllowed(p.to_owned())),
121-
tauri::fs::Event::PathForbidden(p) => f(&Event::PathForbidden(p.to_owned())),
122-
})
123-
} else {
124-
0
125-
}
126-
}
14+
#[derive(Deserialize)]
15+
#[serde(untagged)]
16+
pub(crate) enum EntryRaw {
17+
Value(PathBuf),
18+
Object { path: PathBuf },
12719
}

0 commit comments

Comments
 (0)