mirror of
https://github.com/github/codeql.git
synced 2026-07-08 13:05:36 +02:00
Merge resolvePath into windows-specific file; use (string, bool) API
This commit is contained in:
committed by
GitHub
parent
af2ccd199f
commit
84fb037b2b
1
go/extractor/util/BUILD.bazel
generated
1
go/extractor/util/BUILD.bazel
generated
@@ -9,7 +9,6 @@ go_library(
|
||||
"logging.go",
|
||||
"overlays.go",
|
||||
"semver.go",
|
||||
"subst.go",
|
||||
"subst_other.go",
|
||||
"subst_windows.go",
|
||||
"util.go",
|
||||
|
||||
@@ -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:]
|
||||
}
|
||||
@@ -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 }
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user