Skip to content

Commit a3c049c

Browse files
committed
refactor: some problems are optimized
1 parent 6e1d116 commit a3c049c

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

plugins/fs/guest-js/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,9 @@ async function watchImmediate(
13231323
}
13241324

13251325
/**
1326-
* Get the size of a file or directory.
1326+
* Get the size of a file or directory. For files, the `stat` functions can be used as well.
1327+
*
1328+
* If `path` is a directory, this function will recursively iterate over every file and every directory inside of `path` and therefore will be very time consuming if used on larger directories.
13271329
* @example
13281330
* ```typescript
13291331
* import { size, BaseDirectory } from '@tauri-apps/plugin-fs';
@@ -1332,7 +1334,7 @@ async function watchImmediate(
13321334
* console.log(dirSize); // 1024
13331335
* ```
13341336
*
1335-
* @since 2.0.0
1337+
* @since 2.1.0
13361338
*/
13371339
async function size(path: string | URL): Promise<number> {
13381340
if (path instanceof URL && path.protocol !== 'file:') {

plugins/fs/src/commands.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// SPDX-License-Identifier: Apache-2.0
44
// SPDX-License-Identifier: MIT
55

6-
use anyhow::Ok;
76
use serde::{Deserialize, Serialize, Serializer};
87
use serde_repr::{Deserialize_repr, Serialize_repr};
98
use tauri::{
@@ -919,7 +918,7 @@ pub async fn size<R: Runtime>(
919918
if metadata.is_file() {
920919
Ok(metadata.len())
921920
} else {
922-
let size = get_dir_size(resolved_path).map_err(|e| {
921+
let size = get_dir_size(&resolved_path).map_err(|e| {
923922
format!(
924923
"failed to get size at path: {} with error: {e}",
925924
resolved_path.display()
@@ -930,7 +929,7 @@ pub async fn size<R: Runtime>(
930929
}
931930
}
932931

933-
fn get_dir_size(path: PathBuf) -> CommandResult<u64> {
932+
fn get_dir_size(path: &PathBuf) -> CommandResult<u64> {
934933
let mut size = 0;
935934

936935
for entry in std::fs::read_dir(path)? {
@@ -940,7 +939,7 @@ fn get_dir_size(path: PathBuf) -> CommandResult<u64> {
940939
if metadata.is_file() {
941940
size += metadata.len();
942941
} else if metadata.is_dir() {
943-
size += get_dir_size(entry.path())?;
942+
size += get_dir_size(&entry.path())?;
944943
}
945944
}
946945

0 commit comments

Comments
 (0)