From 03fccdd67b83a8a66fc06e0a8132ca5b151f2d80 Mon Sep 17 00:00:00 2001 From: Sudipto Baral Date: Mon, 22 Sep 2025 10:33:44 -0400 Subject: [PATCH] smb: optimize smb mount performance by avoiding stat checks during initialization add IsPathDir function and tests for trailing slash optimization --- backend/smb/smb.go | 13 ++++++++++ backend/smb/smb_internal_test.go | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 backend/smb/smb_internal_test.go diff --git a/backend/smb/smb.go b/backend/smb/smb.go index 993b2ec7d..afb730332 100644 --- a/backend/smb/smb.go +++ b/backend/smb/smb.go @@ -192,6 +192,9 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e return nil, err } + // if root is empty or ends with / (must be a directory) + isRootDir := isPathDir(root) + root = strings.Trim(root, "/") f := &Fs{ @@ -218,6 +221,11 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e if share == "" || dir == "" { return f, nil } + + // Skip stat check if root is already a directory + if isRootDir { + return f, nil + } cn, err := f.getConnection(ctx, share) if err != nil { return nil, err @@ -894,6 +902,11 @@ func ensureSuffix(s, suffix string) string { return s + suffix } +// isPathDir determines if a path represents a directory based on trailing slash +func isPathDir(path string) bool { + return path == "" || strings.HasSuffix(path, "/") +} + func trimPathPrefix(s, prefix string) string { // we need to clean the paths to make tests pass! s = betterPathClean(s) diff --git a/backend/smb/smb_internal_test.go b/backend/smb/smb_internal_test.go new file mode 100644 index 000000000..d380cd4fd --- /dev/null +++ b/backend/smb/smb_internal_test.go @@ -0,0 +1,41 @@ +// Unit tests for internal SMB functions +package smb + +import "testing" + +// TestIsPathDir tests the isPathDir function logic +func TestIsPathDir(t *testing.T) { + tests := []struct { + path string + expected bool + }{ + // Empty path should be considered a directory + {"", true}, + + // Paths with trailing slash should be directories + {"/", true}, + {"share/", true}, + {"share/dir/", true}, + {"share/dir/subdir/", true}, + + // Paths without trailing slash should not be directories + {"share", false}, + {"share/dir", false}, + {"share/dir/file", false}, + {"share/dir/subdir/file", false}, + + // Edge cases + {"share//", true}, + {"share///", true}, + {"share/dir//", true}, + } + + for _, tt := range tests { + t.Run(tt.path, func(t *testing.T) { + result := isPathDir(tt.path) + if result != tt.expected { + t.Errorf("isPathDir(%q) = %v, want %v", tt.path, result, tt.expected) + } + }) + } +}