From 3c06f01d1871c721dbe0e8c5b8d151c8854904cb Mon Sep 17 00:00:00 2001 From: Zhongcheng Lao Date: Wed, 4 Jun 2025 23:35:44 +0800 Subject: [PATCH] Ensure IsSymlink works on Windows mount point --- pkg/os/filesystem/api.go | 26 ++++++++++---------------- pkg/utils/utils.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/os/filesystem/api.go b/pkg/os/filesystem/api.go index a2fc4d26..3c720cf6 100644 --- a/pkg/os/filesystem/api.go +++ b/pkg/os/filesystem/api.go @@ -33,19 +33,8 @@ func New() API { return filesystemAPI{} } -func pathExists(path string) (bool, error) { - _, err := os.Lstat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} - func (filesystemAPI) PathExists(path string) (bool, error) { - return pathExists(path) + return utils.PathExists(path) } // PathValid determines whether all elements of a path exist @@ -112,18 +101,23 @@ func (filesystemAPI) IsSymlink(tgt string) (bool, error) { // This code is similar to k8s.io/kubernetes/pkg/util/mount except the pathExists usage. // Also in a remote call environment the os error cannot be passed directly back, hence the callers // are expected to perform the isExists check before calling this call in CSI proxy. - stat, err := os.Lstat(tgt) + isSymlink, err := utils.IsPathSymlink(tgt) + if err != nil { + return false, err + } + + // mounted folder created by SetVolumeMountPoint may still report ModeSymlink == 0 + mountedFolder, err := utils.IsMountedFolder(tgt) if err != nil { return false, err } - // If its a link and it points to an existing file then its a mount point. - if stat.Mode()&os.ModeSymlink != 0 { + if isSymlink || mountedFolder { target, err := os.Readlink(tgt) if err != nil { return false, fmt.Errorf("readlink error: %v", err) } - exists, err := pathExists(target) + exists, err := utils.PathExists(target) if err != nil { return false, err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 90c21125..489a28d6 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -109,3 +109,15 @@ func CreateSymlink(link, target string, isDir bool) error { ) return err } + +// PathExists checks whether the given `path` exists. +func PathExists(path string) (bool, error) { + _, err := os.Lstat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +}