Skip to content

Commit 84fb037

Browse files
authored
Merge resolvePath into windows-specific file; use (string, bool) API
1 parent af2ccd1 commit 84fb037

4 files changed

Lines changed: 34 additions & 40 deletions

File tree

go/extractor/util/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/util/subst.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

go/extractor/util/subst_other.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
package util
44

5-
// ResolveDrive is a no-op on non-Windows platforms.
6-
func ResolveDrive(driveRoot string) string { return "" }
5+
// ResolvePath is a no-op on non-Windows platforms.
6+
func ResolvePath(path string) string { return path }

go/extractor/util/subst_windows.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,48 @@ func init() {
3939
available = true
4040
}
4141

42-
// ResolveDrive resolves a subst'd drive root (e.g. "X:\") to its backing path.
43-
// Returns "" if the drive is not subst'd or on error.
44-
func ResolveDrive(driveRoot string) string {
42+
// ResolvePath resolves subst'd drive letters in a full path.
43+
// If the path starts with a subst'd drive letter, replaces it with the backing path.
44+
// Otherwise returns the path unchanged.
45+
func ResolvePath(path string) string {
46+
if len(path) < 3 {
47+
return path
48+
}
49+
if path[1] != ':' {
50+
return path
51+
}
52+
if path[2] != '\\' && path[2] != '/' {
53+
return path
54+
}
55+
c := path[0]
56+
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
57+
return path
58+
}
59+
60+
resolved, ok := resolveDrive(path[:3])
61+
if !ok {
62+
return path
63+
}
64+
return resolved + path[2:]
65+
}
66+
67+
// resolveDrive resolves a subst'd drive root (e.g. "X:\") to its backing path.
68+
// The second return value is false if the drive is not subst'd or on error.
69+
func resolveDrive(driveRoot string) (string, bool) {
4570
if !available {
46-
return ""
71+
return "", false
4772
}
4873
driveBytes, err := windows.ByteSliceFromString(driveRoot)
4974
if err != nil {
50-
return ""
75+
return "", false
5176
}
5277
ret, _, _ := procResolve.Call(uintptr(unsafe.Pointer(&driveBytes[0])))
5378
if ret == 0 {
54-
return ""
79+
return "", false
5580
}
5681
result := windows.BytePtrToString((*byte)(unsafe.Pointer(ret)))
5782
if procFree != nil {
5883
procFree.Call(ret)
5984
}
60-
return result
85+
return result, true
6186
}

0 commit comments

Comments
 (0)