github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/internal/windows/path.go (about) 1 package windows 2 3 import ( 4 "path" 5 "path/filepath" 6 "runtime" 7 "strings" 8 ) 9 10 const windowsGoOS = "windows" 11 12 func HostRunningOnWindows() bool { 13 return runtime.GOOS == windowsGoOS 14 } 15 16 func ToPosix(windowsPath string) (posixPath string) { 17 // volume should be encoded at the start (e.g /c/<path>) where c is the volume 18 volumeName := filepath.VolumeName(windowsPath) 19 pathWithoutVolume := strings.TrimPrefix(windowsPath, volumeName) 20 volumeLetter := strings.ToLower(strings.TrimSuffix(volumeName, ":")) 21 22 // translate non-escaped backslash to forwardslash 23 translatedPath := strings.ReplaceAll(pathWithoutVolume, "\\", "/") 24 25 // always have `/` as the root... join all components, e.g.: 26 // convert: C:\\some\windows\Place 27 // into: /c/some/windows/Place 28 return path.Clean("/" + strings.Join([]string{volumeLetter, translatedPath}, "/")) 29 } 30 31 func FromPosix(posixPath string) (windowsPath string) { 32 // decode the volume (e.g. /c/<path> --> C:\\) - There should always be a volume name. 33 pathFields := strings.Split(posixPath, "/") 34 volumeName := strings.ToUpper(pathFields[1]) + `:\\` 35 36 // translate non-escaped forward slashes into backslashes 37 remainingTranslatedPath := strings.Join(pathFields[2:], "\\") 38 39 // combine volume name and backslash components 40 return filepath.Clean(volumeName + remainingTranslatedPath) 41 }