Repository navigation
feat(UI): implement "Open Data Directory" in the Tauri app #2278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod tauri_http; | ||
| pub mod open_dir; | ||
| pub mod tauri_http; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use log::{debug, error}; | ||
| use serde::Deserialize; | ||
| use std::path::Path; | ||
| use std::time::Duration; | ||
| use tauri::State; | ||
|
|
||
| use crate::portmaster::PORTMASTER_BASE_URL; | ||
|
|
||
| /// Response of the core's `core/paths` endpoint. | ||
| #[derive(Deserialize)] | ||
| struct Paths { | ||
| bin: String, | ||
| data: String, | ||
| logs: String, | ||
| } | ||
|
|
||
| /// The Portmaster directories that the UI may open. | ||
| /// | ||
| /// The UI names a directory by kind only, never by path, so it cannot | ||
| /// open arbitrary locations. | ||
| #[derive(Deserialize, Clone, Copy, Debug)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub enum DirKind { | ||
| Bin, | ||
| Data, | ||
| Logs, | ||
| } | ||
|
|
||
| /// Opens one of the Portmaster directories in the file manager. | ||
| /// | ||
| /// The directories are requested from the core, as only the core knows | ||
| /// them reliably (they may be configured via command line flags). | ||
| #[tauri::command] | ||
| pub async fn open_dir(client: State<'_, reqwest::Client>, kind: DirKind) -> Result<(), String> { | ||
| let paths = client | ||
| .get(format!("{}core/paths", PORTMASTER_BASE_URL)) | ||
| .timeout(Duration::from_secs(10)) | ||
| .send() | ||
| .await | ||
| .and_then(|resp| resp.error_for_status()) | ||
| .map_err(|err| format!("failed to query paths from core: {}", err))? | ||
| .json::<Paths>() | ||
| .await | ||
| .map_err(|err| format!("failed to parse paths from core: {}", err))?; | ||
|
|
||
| let path = match kind { | ||
| DirKind::Bin => paths.bin, | ||
| DirKind::Data => paths.data, | ||
| DirKind::Logs => paths.logs, | ||
| }; | ||
| if path.is_empty() { | ||
| return Err(format!("{:?} directory is not configured", kind)); | ||
| } | ||
|
|
||
| let dir = Path::new(&path); | ||
| if !dir.is_dir() { | ||
| return Err(format!("{:?} directory {} does not exist", kind, path)); | ||
| } | ||
|
|
||
| debug!("opening {:?} directory {}", kind, path); | ||
| open_directory(dir).map_err(|err| { | ||
| error!("failed to open {:?} directory {}: {}", kind, path, err); | ||
| err | ||
| }) | ||
| } | ||
|
|
||
| /// Opens a directory with the file manager registered for folders. | ||
| #[cfg(target_os = "windows")] | ||
| fn open_directory(dir: &Path) -> Result<(), String> { | ||
| use std::os::windows::ffi::OsStrExt; | ||
| use windows::core::PCWSTR; | ||
| use windows::Win32::UI::Shell::ShellExecuteW; | ||
| use windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL; | ||
|
|
||
| // ShellExecuteW with the default verb honors a third-party file manager | ||
| // registered for folders. The `open` crate cannot be used here: the shell | ||
| // plugin enables its `shellexecute-on-windows` feature, and Cargo feature | ||
| // unification applies it to our own `open` dependency as well. With that | ||
| // feature, `open` opens directories via SHOpenFolderAndSelectItems, which | ||
| // always uses Explorer (see issue #1834). | ||
| let path: Vec<u16> = dir | ||
| .as_os_str() | ||
| .encode_wide() | ||
| .chain(std::iter::once(0)) | ||
| .collect(); | ||
|
|
||
| let result = unsafe { | ||
| ShellExecuteW( | ||
| None, | ||
| PCWSTR::null(), | ||
| PCWSTR(path.as_ptr()), | ||
| PCWSTR::null(), | ||
| PCWSTR::null(), | ||
| SW_SHOWNORMAL, | ||
| ) | ||
| }; | ||
|
|
||
| // Per the Win32 documentation, values greater than 32 indicate success. | ||
| if result.0 as usize > 32 { | ||
| Ok(()) | ||
| } else { | ||
| Err(format!( | ||
| "ShellExecuteW failed with code {}", | ||
| result.0 as usize | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| /// Opens a directory with the default file manager (xdg-open and fallbacks). | ||
| #[cfg(not(target_os = "windows"))] | ||
| fn open_directory(dir: &Path) -> Result<(), String> { | ||
| open::that_detached(dir).map_err(|err| format!("failed to open directory: {}", err)) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -113,6 +113,16 @@ | |||||
| return err | ||||||
| } | ||||||
|
|
||||||
| if err := api.RegisterEndpoint(api.Endpoint{ | ||||||
| Path: "core/paths", | ||||||
| Read: api.PermitUser, | ||||||
| StructFunc: getPaths, | ||||||
| Name: "Get Directories", | ||||||
| Description: "Returns the directories used by Portmaster: binaries (bin), variable data (data) and log files (logs).", | ||||||
| }); err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| if err := api.RegisterEndpoint(api.Endpoint{ | ||||||
| Path: "updates/check", | ||||||
| WriteMethod: "POST", | ||||||
|
|
@@ -346,6 +356,14 @@ | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| func getPaths(_ *api.Request) (interface{}, error) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: cat .golangci.yml
sed -n '30,60p' .github/workflows/go.yml
sed -n '345,375p' service/core/api.goRepository: safing/portmaster Length of output: 3025 Replace The repository enables all linters by default and does not disable Suggested fix-func getPaths(_ *api.Request) (interface{}, error) {
+func getPaths(_ *api.Request) (any, error) {📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: Linter[failure] 359-359: 🤖 Prompt for AI Agents |
||||||
| return map[string]string{ | ||||||
| "bin": module.instance.BinDir(), | ||||||
| "data": module.instance.DataDir(), | ||||||
| "logs": module.instance.LogDir(), | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| func getMyProfile(ar *api.Request) (interface{}, error) { | ||||||
| proc, err := process.GetProcessByRequestOrigin(ar) | ||||||
| if err != nil { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: safing/portmaster
Length of output: 408
🏁 Script executed:
Repository: safing/portmaster
Length of output: 6302
Fix the
noinlineerrlint failure..golangci.ymlenables all linters by default and does not disablenoinlineerr. CI runs withonly-new-issues: true, so this new inline error declaration is in scope. The configured exclusions do not includeservice/core/api.go.Use a separate error assignment:
Suggested fix
📝 Committable suggestion
🧰 Tools
🪛 GitHub Check: Linter
[failure] 116-116:
avoid inline error handling using
if err := ...; err != nil; use plain assignmenterr := ...(noinlineerr)🤖 Prompt for AI Agents