Skip to content

Commit

Permalink
os: TempDir uses GetTempPath2 on Windows if available
Browse files Browse the repository at this point in the history
This generates GetTempPath2. Go now tries to determine if the windows
it runs on has GetTempPath2 by finding it only once at the loading time.
If GetTempPath2 exists, it sets the flag so that any calls to tempDir
will use it. If it doesn't exist, Go then uses GetTempPath.
GetTempPath2 was generated into internal/syscall/windows since
syscall is locked down.

Fixes golang#56899
  • Loading branch information
nwnt authored and Nont Thanonchai committed Jan 26, 2023
1 parent 707f888 commit 34a8a96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/internal/syscall/windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const (
//sys GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
//sys SetFileInformationByHandle(handle syscall.Handle, fileInformationClass uint32, buf uintptr, bufsize uint32) (err error) = kernel32.SetFileInformationByHandle
//sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
//sys GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W

const (
// flags for CreateToolhelp32Snapshot
Expand Down Expand Up @@ -363,6 +364,13 @@ func LoadGetFinalPathNameByHandle() error {
return procGetFinalPathNameByHandleW.Find()
}

func LoadGetTempPath2() bool {
if err := procGetTempPath2W.Find(); err != nil {
return false
}
return true
}

//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock

Expand Down
10 changes: 10 additions & 0 deletions src/internal/syscall/windows/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"unsafe"
)

var (
tempPath2Exists bool
)

func init() {
tempPath2Exists = windows.LoadGetTempPath2()
}

// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
Expand Down Expand Up @@ -300,10 +308,14 @@ func Pipe() (r *File, w *File, err error) {
}

func tempDir() string {
getTempPath := syscall.GetTempPath
if tempPath2Exists {
getTempPath = windows.GetTempPath2
}
n := uint32(syscall.MAX_PATH)
for {
b := make([]uint16, n)
n, _ = syscall.GetTempPath(uint32(len(b)), &b[0])
n, _ = getTempPath(uint32(len(b)), &b[0])
if n > uint32(len(b)) {
continue
}
Expand Down

0 comments on commit 34a8a96

Please sign in to comment.