diff --git a/go/extractor/util/BUILD.bazel b/go/extractor/util/BUILD.bazel index 3123c1ac39d..5735e65cf9f 100644 --- a/go/extractor/util/BUILD.bazel +++ b/go/extractor/util/BUILD.bazel @@ -9,7 +9,6 @@ go_library( "logging.go", "overlays.go", "semver.go", - "subst.go", "subst_other.go", "subst_windows.go", "util.go", diff --git a/go/extractor/util/subst.go b/go/extractor/util/subst.go deleted file mode 100644 index 82dcffc495f..00000000000 --- a/go/extractor/util/subst.go +++ /dev/null @@ -1,30 +0,0 @@ -package util - -// ResolvePath resolves subst'd drive letters in a full path. -// If the path starts with a subst'd drive letter, replaces it with the backing path. -// Otherwise returns the path unchanged. -func ResolvePath(path string) string { - return resolvePath(path, ResolveDrive) -} - -func resolvePath(path string, resolveDrive func(string) string) string { - if len(path) < 3 { - return path - } - if path[1] != ':' { - return path - } - if path[2] != '\\' && path[2] != '/' { - return path - } - c := path[0] - if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { - return path - } - - resolved := resolveDrive(path[:3]) - if resolved == "" { - return path - } - return resolved + path[2:] -} diff --git a/go/extractor/util/subst_other.go b/go/extractor/util/subst_other.go index f1789afdaa7..b32420a7746 100644 --- a/go/extractor/util/subst_other.go +++ b/go/extractor/util/subst_other.go @@ -2,5 +2,5 @@ package util -// ResolveDrive is a no-op on non-Windows platforms. -func ResolveDrive(driveRoot string) string { return "" } +// ResolvePath is a no-op on non-Windows platforms. +func ResolvePath(path string) string { return path } diff --git a/go/extractor/util/subst_windows.go b/go/extractor/util/subst_windows.go index a5bf2d7380f..c6c5efc68a4 100644 --- a/go/extractor/util/subst_windows.go +++ b/go/extractor/util/subst_windows.go @@ -39,23 +39,48 @@ func init() { available = true } -// ResolveDrive resolves a subst'd drive root (e.g. "X:\") to its backing path. -// Returns "" if the drive is not subst'd or on error. -func ResolveDrive(driveRoot string) string { +// ResolvePath resolves subst'd drive letters in a full path. +// If the path starts with a subst'd drive letter, replaces it with the backing path. +// Otherwise returns the path unchanged. +func ResolvePath(path string) string { + if len(path) < 3 { + return path + } + if path[1] != ':' { + return path + } + if path[2] != '\\' && path[2] != '/' { + return path + } + c := path[0] + if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { + return path + } + + resolved, ok := resolveDrive(path[:3]) + if !ok { + return path + } + return resolved + path[2:] +} + +// resolveDrive resolves a subst'd drive root (e.g. "X:\") to its backing path. +// The second return value is false if the drive is not subst'd or on error. +func resolveDrive(driveRoot string) (string, bool) { if !available { - return "" + return "", false } driveBytes, err := windows.ByteSliceFromString(driveRoot) if err != nil { - return "" + return "", false } ret, _, _ := procResolve.Call(uintptr(unsafe.Pointer(&driveBytes[0]))) if ret == 0 { - return "" + return "", false } result := windows.BytePtrToString((*byte)(unsafe.Pointer(ret))) if procFree != nil { procFree.Call(ret) } - return result + return result, true }