Skip to content

Commit 214628f

Browse files
author
Nont Thanonchai
committed
os: TempDir uses GetTempPath2 on Windows if available
This generates GetTempPath2 together with RtlGetNtVersionNumbers. The latter is needed to determine if the running Windows has GetTempPath2 by comparing it against the minimum build number that has the API. RtlGetNtVersionNumbers was generated into syscall/windows since syscall is locked down. Fixes #56899
1 parent 707f888 commit 214628f

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/internal/syscall/windows/syscall_windows.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ const (
151151
//sys GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
152152
//sys SetFileInformationByHandle(handle syscall.Handle, fileInformationClass uint32, buf uintptr, bufsize uint32) (err error) = kernel32.SetFileInformationByHandle
153153
//sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
154+
//sys GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W
154155

155156
const (
156157
// flags for CreateToolhelp32Snapshot
@@ -363,6 +364,13 @@ func LoadGetFinalPathNameByHandle() error {
363364
return procGetFinalPathNameByHandleW.Find()
364365
}
365366

367+
func LoadGetTempPath2() bool {
368+
if err := procGetTempPath2W.Find(); err != nil {
369+
return false
370+
}
371+
return true
372+
}
373+
366374
//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
367375
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
368376

src/internal/syscall/windows/zsyscall_windows.go

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

src/os/file_windows.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import (
1414
"unsafe"
1515
)
1616

17+
var (
18+
tempPath2Exists bool
19+
)
20+
21+
func init() {
22+
tempPath2Exists = windows.LoadGetTempPath2()
23+
}
24+
1725
// file is the real representation of *File.
1826
// The extra level of indirection ensures that no clients of os
1927
// can overwrite this data, which could cause the finalizer
@@ -300,10 +308,14 @@ func Pipe() (r *File, w *File, err error) {
300308
}
301309

302310
func tempDir() string {
311+
getTempPath := syscall.GetTempPath
312+
if tempPath2Exists {
313+
getTempPath = windows.GetTempPath2
314+
}
303315
n := uint32(syscall.MAX_PATH)
304316
for {
305317
b := make([]uint16, n)
306-
n, _ = syscall.GetTempPath(uint32(len(b)), &b[0])
318+
n, _ = getTempPath(uint32(len(b)), &b[0])
307319
if n > uint32(len(b)) {
308320
continue
309321
}

0 commit comments

Comments
 (0)