From a3c049c4e27df5ba2fe117785a1afa0cd8defcc1 Mon Sep 17 00:00:00 2001 From: ayang <473033518@qq.com> Date: Tue, 26 Nov 2024 10:48:32 +0800 Subject: [PATCH] refactor: some problems are optimized --- plugins/fs/guest-js/index.ts | 6 ++++-- plugins/fs/src/commands.rs | 7 +++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/fs/guest-js/index.ts b/plugins/fs/guest-js/index.ts index 8af720a4..71f71578 100644 --- a/plugins/fs/guest-js/index.ts +++ b/plugins/fs/guest-js/index.ts @@ -1323,7 +1323,9 @@ async function watchImmediate( } /** - * Get the size of a file or directory. + * Get the size of a file or directory. For files, the `stat` functions can be used as well. + * + * 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. * @example * ```typescript * import { size, BaseDirectory } from '@tauri-apps/plugin-fs'; @@ -1332,7 +1334,7 @@ async function watchImmediate( * console.log(dirSize); // 1024 * ``` * - * @since 2.0.0 + * @since 2.1.0 */ async function size(path: string | URL): Promise { if (path instanceof URL && path.protocol !== 'file:') { diff --git a/plugins/fs/src/commands.rs b/plugins/fs/src/commands.rs index 2088236f..29d1104f 100644 --- a/plugins/fs/src/commands.rs +++ b/plugins/fs/src/commands.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -use anyhow::Ok; use serde::{Deserialize, Serialize, Serializer}; use serde_repr::{Deserialize_repr, Serialize_repr}; use tauri::{ @@ -919,7 +918,7 @@ pub async fn size( if metadata.is_file() { Ok(metadata.len()) } else { - let size = get_dir_size(resolved_path).map_err(|e| { + let size = get_dir_size(&resolved_path).map_err(|e| { format!( "failed to get size at path: {} with error: {e}", resolved_path.display() @@ -930,7 +929,7 @@ pub async fn size( } } -fn get_dir_size(path: PathBuf) -> CommandResult { +fn get_dir_size(path: &PathBuf) -> CommandResult { let mut size = 0; for entry in std::fs::read_dir(path)? { @@ -940,7 +939,7 @@ fn get_dir_size(path: PathBuf) -> CommandResult { if metadata.is_file() { size += metadata.len(); } else if metadata.is_dir() { - size += get_dir_size(entry.path())?; + size += get_dir_size(&entry.path())?; } }