Skip to content

Commit 97b83bc

Browse files
nwntNont Thanonchai
authored and
Nont Thanonchai
committed
os: use GetTempPath2 on Windows if available
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 #56899
1 parent 707f888 commit 97b83bc

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/internal/syscall/windows/syscall_windows.go

+5
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,10 @@ func LoadGetFinalPathNameByHandle() error {
363364
return procGetFinalPathNameByHandleW.Find()
364365
}
365366

367+
func ErrorLoadingGetTempPath2() error {
368+
return procGetTempPath2W.Find()
369+
}
370+
366371
//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
367372
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
368373

src/internal/syscall/windows/zsyscall_windows.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/os/file_windows.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"internal/poll"
1010
"internal/syscall/windows"
1111
"runtime"
12+
"sync"
1213
"syscall"
1314
"unicode/utf16"
1415
"unsafe"
@@ -299,11 +300,23 @@ func Pipe() (r *File, w *File, err error) {
299300
return newFile(p[0], "|0", "pipe"), newFile(p[1], "|1", "pipe"), nil
300301
}
301302

303+
var (
304+
useGetTempPath2Once sync.Once
305+
useGetTempPath2 bool
306+
)
307+
302308
func tempDir() string {
309+
useGetTempPath2Once.Do(func() {
310+
useGetTempPath2 = (windows.ErrorLoadingGetTempPath2() == nil)
311+
})
312+
getTempPath := syscall.GetTempPath
313+
if useGetTempPath2 {
314+
getTempPath = windows.GetTempPath2
315+
}
303316
n := uint32(syscall.MAX_PATH)
304317
for {
305318
b := make([]uint16, n)
306-
n, _ = syscall.GetTempPath(uint32(len(b)), &b[0])
319+
n, _ = getTempPath(uint32(len(b)), &b[0])
307320
if n > uint32(len(b)) {
308321
continue
309322
}

0 commit comments

Comments
 (0)