From b42597af8d6640312968bcde943d84e8a295cba6 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Tue, 10 Dec 2024 08:56:37 +0000 Subject: [PATCH] cleanup: windows functions --- .../safe_mounter_host_process_windows.go | 6 ++--- pkg/os/filesystem/filesystem.go | 27 +------------------ pkg/os/volume/volume.go | 11 -------- 3 files changed, 4 insertions(+), 40 deletions(-) diff --git a/pkg/mounter/safe_mounter_host_process_windows.go b/pkg/mounter/safe_mounter_host_process_windows.go index 44ae476987..12f7d5541e 100644 --- a/pkg/mounter/safe_mounter_host_process_windows.go +++ b/pkg/mounter/safe_mounter_host_process_windows.go @@ -51,8 +51,8 @@ func NewWinMounter(listDisksUsingWinCIM bool) *winMounter { } // Mount just creates a soft link at target pointing to source. -func (mounter *winMounter) Mount(source, target, fstype string, options []string) error { - return filesystem.LinkPath(normalizeWindowsPath(source), normalizeWindowsPath(target)) +func (mounter *winMounter) Mount(source, target, _ string, _ []string) error { + return os.Symlink(normalizeWindowsPath(source), normalizeWindowsPath(target)) } // Rmdir - delete the given directory @@ -111,7 +111,7 @@ func (mounter *winMounter) IsLikelyNotMountPoint(path string) (bool, error) { // Currently the make dir is only used from the staging code path, hence we call it // with Plugin context.. func (mounter *winMounter) MakeDir(path string) error { - return filesystem.Mkdir(normalizeWindowsPath(path)) + return os.MkdirAll(normalizeWindowsPath(path), 0755) } // ExistsPath - Checks if a path exists. Unlike util ExistsPath, this call does not perform follow link. diff --git a/pkg/os/filesystem/filesystem.go b/pkg/os/filesystem/filesystem.go index 825bd47e61..76ff2d403b 100644 --- a/pkg/os/filesystem/filesystem.go +++ b/pkg/os/filesystem/filesystem.go @@ -104,13 +104,6 @@ func ValidatePathWindows(path string) error { return nil } -func Mkdir(path string) error { - if err := ValidatePathWindows(path); err != nil { - return err - } - return os.MkdirAll(path, 0755) -} - func Rmdir(path string, force bool) error { if err := ValidatePathWindows(path); err != nil { return err @@ -122,28 +115,10 @@ func Rmdir(path string, force bool) error { return os.Remove(path) } -func LinkPath(sourcePath, targetPath string) error { - return CreateSymlink(sourcePath, targetPath) -} - -func CreateSymlink(sourcePath, targetPath string) error { - if err := ValidatePathWindows(targetPath); err != nil { - return err - } - if err := ValidatePathWindows(sourcePath); err != nil { - return err - } - return os.Symlink(sourcePath, targetPath) -} - func IsMountPoint(path string) (bool, error) { return IsSymlink(path) } -func IsSymlink(path string) (bool, error) { - return isSymlink(path) -} - // IsSymlink - returns true if tgt is a mount point. // A path is considered a mount point if: // - directory exists and @@ -151,7 +126,7 @@ func IsSymlink(path string) (bool, error) { // - the target path of the link exists. // If tgt path does not exist, it returns an error // if tgt path exists, but the source path tgt points to does not exist, it returns false without error. -func isSymlink(tgt string) (bool, error) { +func IsSymlink(tgt string) (bool, error) { // This code is similar to k8s.io/kubernetes/pkg/util/mount except the pathExists usage. stat, err := os.Lstat(tgt) if err != nil { diff --git a/pkg/os/volume/volume.go b/pkg/os/volume/volume.go index fe26d43b14..11a5327d21 100644 --- a/pkg/os/volume/volume.go +++ b/pkg/os/volume/volume.go @@ -28,17 +28,6 @@ import ( "sigs.k8s.io/azuredisk-csi-driver/pkg/azureutils" ) -var ( - // VolumeRegexp matches a Windows Volume - // example: Volume{452e318a-5cde-421e-9831-b9853c521012} - // - // The field UniqueId has an additional prefix which is NOT included in the regex - // however the regex can match UniqueId too - // PS C:\disks> (Get-Disk -Number 1 | Get-Partition | Get-Volume).UniqueId - // \\?\Volume{452e318a-5cde-421e-9831-b9853c521012}\ - VolumeRegexp = regexp.MustCompile(`Volume\{[\w-]*\}`) -) - func getVolumeSize(volumeID string) (int64, error) { cmd := "(Get-Volume -UniqueId \"$Env:volumeID\" | Get-partition).Size" out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))