From bb49a05fb5aea7343d2f1bbfe4eb33f17bb5735b Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Mon, 23 Dec 2024 09:35:34 +0000 Subject: [PATCH] fix: adopt go1.23 behavior change in mount point parsing on Windows fix comments --- pkg/util/filesystem/defaultfs.go | 2 +- pkg/volume/util/fs/fs_windows.go | 5 +++++ pkg/volume/util/subpath/subpath_windows.go | 10 ++++++++++ .../util/volumepathhandler/volume_path_handler.go | 7 ++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/util/filesystem/defaultfs.go b/pkg/util/filesystem/defaultfs.go index ef99bd3bc46..0dfd11fb435 100644 --- a/pkg/util/filesystem/defaultfs.go +++ b/pkg/util/filesystem/defaultfs.go @@ -90,7 +90,7 @@ func MkdirAllWithPathCheck(path string, perm os.FileMode) error { // 1. for Unix/Linux OS, check if the path is directory. // 2. for windows NTFS, check if the path is symlink instead of directory. if dir.IsDir() || - (runtime.GOOS == "windows" && (dir.Mode()&os.ModeSymlink != 0)) { + (runtime.GOOS == "windows" && (dir.Mode()&os.ModeSymlink != 0 || dir.Mode()&os.ModeIrregular != 0)) { return nil } return fmt.Errorf("path %v exists but is not a directory", path) diff --git a/pkg/volume/util/fs/fs_windows.go b/pkg/volume/util/fs/fs_windows.go index 6e138514a6a..3e654d3fd3d 100644 --- a/pkg/volume/util/fs/fs_windows.go +++ b/pkg/volume/util/fs/fs_windows.go @@ -85,6 +85,11 @@ func diskUsage(currPath string, info os.FileInfo) (int64, error) { return size, nil } + // go1.23 behavior change: https://github.com/golang/go/issues/63703#issuecomment-2535941458 + if info.Mode()&os.ModeIrregular != 0 { + return size, nil + } + size += info.Size() if !info.IsDir() { diff --git a/pkg/volume/util/subpath/subpath_windows.go b/pkg/volume/util/subpath/subpath_windows.go index bf02de632fb..e23746ff0bb 100644 --- a/pkg/volume/util/subpath/subpath_windows.go +++ b/pkg/volume/util/subpath/subpath_windows.go @@ -208,6 +208,12 @@ func lockAndCheckSubPathWithoutSymlink(volumePath, subPath string) ([]uintptr, e break } + // go1.23 behavior change: https://github.com/golang/go/issues/63703#issuecomment-2535941458 + if stat.Mode()&os.ModeIrregular != 0 { + errorResult = fmt.Errorf("subpath %q is an unexpected irregular file after EvalSymlinks", currentFullPath) + break + } + if !mount.PathWithinBase(currentFullPath, volumePath) { errorResult = fmt.Errorf("SubPath %q not within volume path %q", currentFullPath, volumePath) break @@ -342,6 +348,10 @@ func doSafeMakeDir(pathname string, base string, perm os.FileMode) error { if stat.Mode()&os.ModeSymlink != 0 { return fmt.Errorf("subpath %q is an unexpected symlink after Mkdir", currentPath) } + // go1.23 behavior change: https://github.com/golang/go/issues/63703#issuecomment-2535941458 + if stat.Mode()&os.ModeIrregular != 0 { + return fmt.Errorf("subpath %q is an unexpected irregular file after Mkdir", currentPath) + } } return nil diff --git a/pkg/volume/util/volumepathhandler/volume_path_handler.go b/pkg/volume/util/volumepathhandler/volume_path_handler.go index e632843d1e1..fa0b24a18d8 100644 --- a/pkg/volume/util/volumepathhandler/volume_path_handler.go +++ b/pkg/volume/util/volumepathhandler/volume_path_handler.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "k8s.io/klog/v2" "k8s.io/mount-utils" @@ -232,7 +233,7 @@ func (v VolumePathHandler) RemoveMapPath(mapPath string) error { return nil } -// IsSymlinkExist returns true if specified file exists and the type is symbolik link. +// IsSymlinkExist returns true if specified file exists and the type is symbolik link or irregular file on Windows. // If file doesn't exist, or file exists but not symbolic link, return false with no error. // On other cases, return false with error from Lstat(). func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) { @@ -249,6 +250,10 @@ func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) { if fi.Mode()&os.ModeSymlink == os.ModeSymlink { return true, nil } + // go1.23 behavior change: https://github.com/golang/go/issues/63703#issuecomment-2535941458 + if (runtime.GOOS == "windows") && (fi.Mode()&os.ModeIrregular != 0) { + return true, nil + } // If file exits but it's not symbolic link, return false and no error return false, nil }