Skip to content

Commit

Permalink
feat(core): enhance readDir error message, closes #7379 (#7416)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 13, 2023
1 parent 2eab150 commit c982733
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changes/enhance-read-dir-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Enhance `readDir` API error with path information.
25 changes: 8 additions & 17 deletions core/tauri/src/api/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,10 @@ pub fn is_dir<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
}

fn is_symlink<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
// TODO: remove the different implementation once we raise tauri's MSRV to at least 1.58
#[cfg(windows)]
let ret = symlink_metadata(path)
let path = path.as_ref();
symlink_metadata(path)
.map(|md| md.is_symlink())
.map_err(Into::into);

#[cfg(not(windows))]
let ret = symlink_metadata(path)
.map(|md| md.file_type().is_symlink())
.map_err(Into::into);

ret
.map_err(|e| crate::api::Error::PathIo(e, path.to_path_buf()))
}

/// Reads a directory. Can perform recursive operations.
Expand All @@ -62,20 +54,19 @@ pub(crate) fn read_dir_with_options<P: AsRef<Path>>(
options: ReadDirOptions<'_>,
) -> crate::api::Result<Vec<DiskEntry>> {
let mut files_and_dirs: Vec<DiskEntry> = vec![];
for entry in fs::read_dir(path)? {
let path = path.as_ref();
for entry in fs::read_dir(path).map_err(|e| crate::api::Error::PathIo(e, path.to_path_buf()))? {
let path = entry?.path();
let path_as_string = path.display().to_string();

if let Ok(flag) = is_dir(&path_as_string) {
if let Ok(flag) = is_dir(&path) {
files_and_dirs.push(DiskEntry {
path: path.clone(),
children: if flag {
Some(
if recursive
&& (!is_symlink(&path_as_string)?
|| options.scope.map(|s| s.is_allowed(&path)).unwrap_or(true))
&& (!is_symlink(&path)? || options.scope.map(|s| s.is_allowed(&path)).unwrap_or(true))
{
read_dir_with_options(&path_as_string, true, options)?
read_dir_with_options(&path, true, options)?
} else {
vec![]
},
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub enum Error {
/// IO error.
#[error(transparent)]
Io(#[from] std::io::Error),
/// IO error at the given path.
#[error("{0}, path {1}")]
PathIo(std::io::Error, std::path::PathBuf),
/// Ignore error.
#[error("failed to walkdir: {0}")]
Ignore(#[from] ignore::Error),
Expand Down
3 changes: 1 addition & 2 deletions core/tauri/src/endpoints/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,12 @@ impl Cmd {
dir,
)?;
dir::read_dir_with_options(
&resolved_path,
resolved_path,
recursive,
dir::ReadDirOptions {
scope: Some(&context.window.state::<Scopes>().fs),
},
)
.with_context(|| format!("path: {}", resolved_path.display()))
.map_err(Into::into)
}

Expand Down

0 comments on commit c982733

Please sign in to comment.