Skip to content

Commit

Permalink
enhance: include the path in ACL I/O errors (#11575)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Nov 4, 2024
1 parent 129414f commit c33bbf4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changes/utils-acl-path-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": minor:enhance
"tauri-utils": minor:enhance
---

Include the path in ACL I/O errors.
14 changes: 8 additions & 6 deletions crates/tauri-utils/src/acl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const CORE_PLUGIN_PERMISSIONS_TOKEN: &str = "__CORE_PLUGIN__";
fn parse_permissions(paths: Vec<PathBuf>) -> Result<Vec<PermissionFile>, Error> {
let mut permissions = Vec::new();
for path in paths {
let permission_file = fs::read_to_string(&path).map_err(Error::ReadFile)?;
let ext = path.extension().unwrap().to_string_lossy().to_string();
let permission_file = fs::read_to_string(&path).map_err(|e| Error::ReadFile(e, path))?;
let permission: PermissionFile = match ext.as_str() {
"toml" => toml::from_str(&permission_file)?,
"json" => serde_json::from_str(&permission_file)?,
Expand Down Expand Up @@ -81,7 +81,8 @@ pub fn define_permissions<F: Fn(&Path) -> bool>(
let pkg_name_valid_path = pkg_name.replace(':', "-");
let permission_files_path = out_dir.join(format!("{}-permission-files", pkg_name_valid_path));
let permission_files_json = serde_json::to_string(&permission_files)?;
fs::write(&permission_files_path, permission_files_json).map_err(Error::WriteFile)?;
fs::write(&permission_files_path, permission_files_json)
.map_err(|e| Error::WriteFile(e, permission_files_path.clone()))?;

if let Some(plugin_name) = pkg_name.strip_prefix("tauri:") {
println!(
Expand Down Expand Up @@ -115,7 +116,8 @@ pub fn read_permissions() -> Result<HashMap<String, Vec<PermissionFile>>, Error>
})
{
let permissions_path = PathBuf::from(value);
let permissions_str = fs::read_to_string(&permissions_path).map_err(Error::ReadFile)?;
let permissions_str =
fs::read_to_string(&permissions_path).map_err(|e| Error::ReadFile(e, permissions_path))?;
let permissions: Vec<PathBuf> = serde_json::from_str(&permissions_str)?;
let permissions = parse_permissions(permissions)?;

Expand All @@ -139,7 +141,7 @@ pub fn define_global_scope_schema(
out_dir: &Path,
) -> Result<(), Error> {
let path = out_dir.join("global-scope.json");
fs::write(&path, serde_json::to_vec(&schema)?).map_err(Error::WriteFile)?;
fs::write(&path, serde_json::to_vec(&schema)?).map_err(|e| Error::WriteFile(e, path.clone()))?;

if let Some(plugin_name) = pkg_name.strip_prefix("tauri:") {
println!(
Expand Down Expand Up @@ -170,7 +172,7 @@ pub fn read_global_scope_schemas() -> Result<HashMap<String, serde_json::Value>,
})
{
let path = PathBuf::from(value);
let json = fs::read_to_string(&path).map_err(Error::ReadFile)?;
let json = fs::read_to_string(&path).map_err(|e| Error::ReadFile(e, path))?;
let schema: serde_json::Value = serde_json::from_str(&json)?;

let plugin_crate_name = plugin_crate_name_var.to_lowercase().replace('_', "-");
Expand Down Expand Up @@ -368,7 +370,7 @@ pub fn generate_docs(
format!("{default_permission}\n{PERMISSION_TABLE_HEADER}\n{permission_table}</table>\n");

let reference_path = out_dir.join(PERMISSION_DOCS_FILE_NAME);
write_if_changed(reference_path, docs).map_err(Error::WriteFile)?;
write_if_changed(&reference_path, docs).map_err(|e| Error::WriteFile(e, reference_path))?;

Ok(())
}
3 changes: 2 additions & 1 deletion crates/tauri-utils/src/acl/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ impl CapabilityFile {
/// Load the given capability file.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, super::Error> {
let path = path.as_ref();
let capability_file = std::fs::read_to_string(path).map_err(super::Error::ReadFile)?;
let capability_file =
std::fs::read_to_string(path).map_err(|e| super::Error::ReadFile(e, path.into()))?;
let ext = path.extension().unwrap().to_string_lossy().to_string();
let file: Self = match ext.as_str() {
"toml" => toml::from_str(&capability_file)?,
Expand Down
18 changes: 9 additions & 9 deletions crates/tauri-utils/src/acl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! [Struct Update Syntax]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
use serde::{Deserialize, Serialize};
use std::{num::NonZeroU64, str::FromStr, sync::Arc};
use std::{num::NonZeroU64, path::PathBuf, str::FromStr, sync::Arc};
use thiserror::Error;
use url::Url;

Expand Down Expand Up @@ -71,20 +71,20 @@ pub enum Error {
LinksName,

/// IO error while reading a file
#[error("failed to read file: {0}")]
ReadFile(std::io::Error),
#[error("failed to read file '{}': {}", _1.display(), _0)]
ReadFile(std::io::Error, PathBuf),

/// IO error while writing a file
#[error("failed to write file: {0}")]
WriteFile(std::io::Error),
#[error("failed to write file '{}': {}", _1.display(), _0)]
WriteFile(std::io::Error, PathBuf),

/// IO error while creating a file
#[error("failed to create file: {0}")]
CreateFile(std::io::Error),
#[error("failed to create file '{}': {}", _1.display(), _0)]
CreateFile(std::io::Error, PathBuf),

/// IO error while creating a dir
#[error("failed to create dir: {0}")]
CreateDir(std::io::Error),
#[error("failed to create dir '{}': {}", _1.display(), _0)]
CreateDir(std::io::Error, PathBuf),

/// [`cargo_metadata`] was not able to complete successfully
#[cfg(feature = "build")]
Expand Down
4 changes: 2 additions & 2 deletions crates/tauri-utils/src/acl/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ pub fn generate_permissions_schema<P: AsRef<Path>>(
let schema_str = serde_json::to_string_pretty(&schema)?;

let out_dir = out_dir.as_ref().join(PERMISSION_SCHEMAS_FOLDER_NAME);
fs::create_dir_all(&out_dir).map_err(Error::CreateDir)?;
fs::create_dir_all(&out_dir).map_err(|e| Error::CreateDir(e, out_dir.clone()))?;

let schema_path = out_dir.join(PERMISSION_SCHEMA_FILE_NAME);
write_if_changed(&schema_path, schema_str).map_err(Error::WriteFile)?;
write_if_changed(&schema_path, schema_str).map_err(|e| Error::WriteFile(e, schema_path))?;

Ok(())
}

0 comments on commit c33bbf4

Please sign in to comment.