From 0fd36b00bb49257f8ab1b8b4495b06e1883ad68c Mon Sep 17 00:00:00 2001 From: Angela Li Date: Sat, 6 Feb 2021 02:13:31 -0800 Subject: [PATCH 1/2] Escape the special character in vsphere windows path --- pkg/volume/util/subpath/subpath_windows.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/volume/util/subpath/subpath_windows.go b/pkg/volume/util/subpath/subpath_windows.go index 0e9367eaf00..964f86af9f3 100644 --- a/pkg/volume/util/subpath/subpath_windows.go +++ b/pkg/volume/util/subpath/subpath_windows.go @@ -75,7 +75,7 @@ func getUpperPath(path string) string { // Check whether a directory/file is a link type or not // LinkType could be SymbolicLink, Junction, or HardLink func isLinkPath(path string) (bool, error) { - cmd := fmt.Sprintf("(Get-Item -Path %s).LinkType", path) + cmd := fmt.Sprintf("(Get-Item -Path %s).LinkType", escapeWindowsPath(path)) output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() if err != nil { return false, err @@ -86,6 +86,17 @@ func isLinkPath(path string) (bool, error) { return false, nil } +// Escape the special character in vsphere windows path +func escapeWindowsPath(path string) (string) { + if strings.Contains(path, "``[") || strings.Contains(path, "``]") || strings.Contains(path, "` ") { + return path + } + escapeLeft := strings.Replace(path, "[", "``[", -1) + escapeRight := strings.Replace(escapeLeft, "]", "``]", -1) + escapeSpace := strings.Replace(escapeRight, " ", "` ", -1) + return escapeSpace +} + // evalSymlink returns the path name after the evaluation of any symbolic links. // If the path after evaluation is a device path or network connection, the original path is returned func evalSymlink(path string) (string, error) { @@ -113,7 +124,7 @@ func evalSymlink(path string) (string, error) { } } // This command will give the target path of a given symlink - cmd := fmt.Sprintf("(Get-Item -Path %s).Target", upperpath) + cmd := fmt.Sprintf("(Get-Item -Path %s).Target", escapeWindowsPath(upperpath)) output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() if err != nil { return "", err From 1edb548b0934d3bfdd5075655756a7837f5bc039 Mon Sep 17 00:00:00 2001 From: Angela Li Date: Sat, 6 Feb 2021 22:45:46 -0800 Subject: [PATCH 2/2] Use -LiteralPath instead of -Path --- pkg/volume/util/subpath/subpath_windows.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pkg/volume/util/subpath/subpath_windows.go b/pkg/volume/util/subpath/subpath_windows.go index 964f86af9f3..3428e639546 100644 --- a/pkg/volume/util/subpath/subpath_windows.go +++ b/pkg/volume/util/subpath/subpath_windows.go @@ -75,7 +75,7 @@ func getUpperPath(path string) string { // Check whether a directory/file is a link type or not // LinkType could be SymbolicLink, Junction, or HardLink func isLinkPath(path string) (bool, error) { - cmd := fmt.Sprintf("(Get-Item -Path %s).LinkType", escapeWindowsPath(path)) + cmd := fmt.Sprintf("(Get-Item -LiteralPath %q).LinkType", path) output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() if err != nil { return false, err @@ -86,17 +86,6 @@ func isLinkPath(path string) (bool, error) { return false, nil } -// Escape the special character in vsphere windows path -func escapeWindowsPath(path string) (string) { - if strings.Contains(path, "``[") || strings.Contains(path, "``]") || strings.Contains(path, "` ") { - return path - } - escapeLeft := strings.Replace(path, "[", "``[", -1) - escapeRight := strings.Replace(escapeLeft, "]", "``]", -1) - escapeSpace := strings.Replace(escapeRight, " ", "` ", -1) - return escapeSpace -} - // evalSymlink returns the path name after the evaluation of any symbolic links. // If the path after evaluation is a device path or network connection, the original path is returned func evalSymlink(path string) (string, error) { @@ -124,7 +113,7 @@ func evalSymlink(path string) (string, error) { } } // This command will give the target path of a given symlink - cmd := fmt.Sprintf("(Get-Item -Path %s).Target", escapeWindowsPath(upperpath)) + cmd := fmt.Sprintf("(Get-Item -LiteralPath %q).Target", upperpath) output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() if err != nil { return "", err