Skip to content

Commit

Permalink
Escape the special character in vsphere windows path
Browse files Browse the repository at this point in the history
  • Loading branch information
liyanhui1228 authored and yujuhong committed Feb 11, 2021
1 parent a3d4ea9 commit 97dfcaa
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/volume/util/subpath/subpath_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 97dfcaa

Please sign in to comment.