You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/05/07 11:21:36 UTC

(opendal) 01/01: fix(core/services/fs): Returning empty dir while list a file path

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch allow-list-file-endwiths-slash
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit daa454c736fd7b34b9b891904570f2eff158cd9a
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Wed May 7 19:20:27 2025 +0800

    fix(core/services/fs): Returning empty dir while list a file path
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 core/src/services/fs/backend.rs | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/core/src/services/fs/backend.rs b/core/src/services/fs/backend.rs
index 1432b0ee8..ad7f9e6bc 100644
--- a/core/src/services/fs/backend.rs
+++ b/core/src/services/fs/backend.rs
@@ -366,10 +366,17 @@ impl Access for FsBackend {
         let f = match tokio::fs::read_dir(&p).await {
             Ok(rd) => rd,
             Err(e) => {
-                return if e.kind() == std::io::ErrorKind::NotFound {
-                    Ok((RpList::default(), None))
-                } else {
-                    Err(new_std_io_error(e))
+                return match e.kind() {
+                    // Return empty list if the directory not found
+                    std::io::ErrorKind::NotFound => Ok((RpList::default(), None)),
+                    // If the path is not a directory, return an empty list
+                    //
+                    // The path could be a file or a symbolic link in this case.
+                    // Returning a NotADirectory error to the user isn't helpful; instead,
+                    // providing an empty directory is a more user-friendly. In fact, the dir
+                    // `path/` does not exist.
+                    std::io::ErrorKind::NotADirectory => Ok((RpList::default(), None)),
+                    _ => Err(new_std_io_error(e)),
                 };
             }
         };
@@ -541,10 +548,17 @@ impl Access for FsBackend {
         let f = match std::fs::read_dir(p) {
             Ok(rd) => rd,
             Err(e) => {
-                return if e.kind() == std::io::ErrorKind::NotFound {
-                    Ok((RpList::default(), None))
-                } else {
-                    Err(new_std_io_error(e))
+                return match e.kind() {
+                    // Return empty list if the directory not found
+                    std::io::ErrorKind::NotFound => Ok((RpList::default(), None)),
+                    // If the path is not a directory, return an empty list
+                    //
+                    // The path could be a file or a symbolic link in this case.
+                    // Returning a NotADirectory error to the user isn't helpful; instead,
+                    // providing an empty directory is a more user-friendly. In fact, the dir
+                    // `path/` does not exist.
+                    std::io::ErrorKind::NotADirectory => Ok((RpList::default(), None)),
+                    _ => Err(new_std_io_error(e)),
                 };
             }
         };