Skip to content

Commit 3e78173

Browse files
authored
fix(fs): ignore metadata error while reading dir entries (#2018)
* fix(fs): ignore metadata error while reading dir entries closes #2014 * remove package manager
1 parent 64fac08 commit 3e78173

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"fs": "patch"
3+
"fs-js": "patch"
4+
---
5+
6+
Fix `readDir` function failing to read directories that contain broken symlinks.
7+

examples/api/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
2-
"name": "svelte-app",
2+
"name": "api",
33
"private": true,
44
"version": "2.0.2",
55
"type": "module",
66
"scripts": {
77
"dev": "vite --clearScreen false",
88
"build": "vite build",
9-
"serve": "vite preview"
9+
"serve": "vite preview",
10+
"tauri": "tauri"
1011
},
1112
"dependencies": {
1213
"@tauri-apps/api": "2.0.3",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"build": "pnpm run -r --parallel --filter !plugins-workspace --filter !\"./plugins/*/examples/**\" --filter !\"./examples/*\" build",
88
"lint": "eslint .",
99
"format": "prettier --write .",
10-
"format:check": "prettier --check ."
10+
"format:check": "prettier --check .",
11+
"example:api:dev": "pnpm run --filter \"api\" tauri dev"
1112
},
1213
"devDependencies": {
1314
"@eslint/js": "9.14.0",

plugins/fs/src/commands.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
borrow::Cow,
1717
fs::File,
1818
io::{BufReader, Lines, Read, Write},
19-
path::{Path, PathBuf},
19+
path::PathBuf,
2020
str::FromStr,
2121
sync::Mutex,
2222
time::{SystemTime, UNIX_EPOCH},
@@ -245,32 +245,12 @@ pub fn mkdir<R: Runtime>(
245245
#[serde(rename_all = "camelCase")]
246246
#[non_exhaustive]
247247
pub struct DirEntry {
248-
pub name: Option<String>,
248+
pub name: String,
249249
pub is_directory: bool,
250250
pub is_file: bool,
251251
pub is_symlink: bool,
252252
}
253253

254-
fn read_dir_inner<P: AsRef<Path>>(path: P) -> crate::Result<Vec<DirEntry>> {
255-
let mut files_and_dirs: Vec<DirEntry> = vec![];
256-
for entry in std::fs::read_dir(path)? {
257-
let path = entry?.path();
258-
let file_type = path.metadata()?.file_type();
259-
files_and_dirs.push(DirEntry {
260-
is_directory: file_type.is_dir(),
261-
is_file: file_type.is_file(),
262-
is_symlink: std::fs::symlink_metadata(&path)
263-
.map(|md| md.file_type().is_symlink())
264-
.unwrap_or(false),
265-
name: path
266-
.file_name()
267-
.map(|name| name.to_string_lossy())
268-
.map(|name| name.to_string()),
269-
});
270-
}
271-
Result::Ok(files_and_dirs)
272-
}
273-
274254
#[tauri::command]
275255
pub async fn read_dir<R: Runtime>(
276256
webview: Webview<R>,
@@ -287,14 +267,37 @@ pub async fn read_dir<R: Runtime>(
287267
options.as_ref().and_then(|o| o.base_dir),
288268
)?;
289269

290-
read_dir_inner(&resolved_path)
291-
.map_err(|e| {
292-
format!(
293-
"failed to read directory at path: {} with error: {e}",
294-
resolved_path.display()
295-
)
270+
let entries = std::fs::read_dir(&resolved_path).map_err(|e| {
271+
format!(
272+
"failed to read directory at path: {} with error: {e}",
273+
resolved_path.display()
274+
)
275+
})?;
276+
277+
let entries = entries
278+
.filter_map(|entry| {
279+
let entry = entry.ok()?;
280+
let name = entry.file_name().into_string().ok()?;
281+
let metadata = entry.file_type();
282+
macro_rules! method_or_false {
283+
($method:ident) => {
284+
if let Ok(metadata) = &metadata {
285+
metadata.$method()
286+
} else {
287+
false
288+
}
289+
};
290+
}
291+
Some(DirEntry {
292+
name,
293+
is_file: method_or_false!(is_file),
294+
is_directory: method_or_false!(is_dir),
295+
is_symlink: method_or_false!(is_symlink),
296+
})
296297
})
297-
.map_err(Into::into)
298+
.collect();
299+
300+
Ok(entries)
298301
}
299302

300303
#[tauri::command]

0 commit comments

Comments
 (0)