Merge resolvePath into windows-specific file; use (string, bool) API

This commit is contained in:
copilot-swe-agent[bot]
2026-07-07 12:09:43 +00:00
committed by GitHub
parent af2ccd199f
commit 84fb037b2b
4 changed files with 34 additions and 40 deletions

View File

@@ -9,7 +9,6 @@ go_library(
"logging.go",
"overlays.go",
"semver.go",
"subst.go",
"subst_other.go",
"subst_windows.go",
"util.go",

View File

@@ -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:]
}

View File

@@ -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 }

View File

@@ -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
}