Skip to content

path/filepath: try GetFinalPathNameByHandle with VOLUME_NAME_GUID #40095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func openSymlink(path string) (syscall.Handle, error) {
// \??\C:\foo\bar into C:\foo\bar
// \??\UNC\foo\bar into \\foo\bar
// \??\Volume{abc}\ into C:\
// \??\Volume{abc}\ into \\?\Volume{abc} if GetFinalPathNameByHandle(,,,VOLUME_NAME_DOS) returns PATH_NOT_FOUND (3)
func normaliseLinkPath(path string) (string, error) {
if len(path) < 4 || path[:4] != `\??\` {
// unexpected path, return it as is
Expand Down Expand Up @@ -431,8 +432,15 @@ func normaliseLinkPath(path string) (string, error) {
defer syscall.CloseHandle(h)

buf := make([]uint16, 100)
isVolumeNameGUID := false
for {
n, err := windows.GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), windows.VOLUME_NAME_DOS)
if errors.Is(err, ErrNotExist) {
n, err = windows.GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), windows.VOLUME_NAME_GUID)
if err == nil {
isVolumeNameGUID = true
}
}
if err != nil {
return "", err
}
Expand All @@ -443,7 +451,9 @@ func normaliseLinkPath(path string) (string, error) {
}
s = syscall.UTF16ToString(buf)
if len(s) > 4 && s[:4] == `\\?\` {
s = s[4:]
if !isVolumeNameGUID {
s = s[4:]
}
if len(s) > 3 && s[:3] == `UNC` {
// return path like \\server\share\...
return `\` + s[3:], nil
Expand Down
6 changes: 6 additions & 0 deletions src/path/filepath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ func Ext(path string) string {
// If path is relative the result will be relative to the current directory,
// unless one of the components is an absolute symbolic link.
// EvalSymlinks calls Clean on the result.
// Use of this function is unsuitable for applications.
// This function always resolves all links on path, but links introduce
// indirections that may solve problems that are not the concern of applications
// and of which applications should remain unaware unless they perform
// system administrative tasks on behalf of the user in which case they should
// resolve only those links that they create, manage and delete.
func EvalSymlinks(path string) (string, error) {
return evalSymlinks(path)
}
Expand Down