Skip to content

Commit b779389

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 1780807 commit b779389

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/internal/syscall/windows/syscall_windows.go

Lines changed: 5 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,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

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: 14 additions & 1 deletion
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"
@@ -230,11 +231,23 @@ func Pipe() (r *File, w *File, err error) {
230231
return newFile(p[0], "|0", "pipe"), newFile(p[1], "|1", "pipe"), nil
231232
}
232233

234+
var (
235+
useGetTempPath2Once sync.Once
236+
useGetTempPath2 bool
237+
)
238+
233239
func tempDir() string {
240+
useGetTempPath2Once.Do(func() {
241+
useGetTempPath2 = (windows.ErrorLoadingGetTempPath2() == nil)
242+
})
243+
getTempPath := syscall.GetTempPath
244+
if useGetTempPath2 {
245+
getTempPath = windows.GetTempPath2
246+
}
234247
n := uint32(syscall.MAX_PATH)
235248
for {
236249
b := make([]uint16, n)
237-
n, _ = syscall.GetTempPath(uint32(len(b)), &b[0])
250+
n, _ = getTempPath(uint32(len(b)), &b[0])
238251
if n > uint32(len(b)) {
239252
continue
240253
}

0 commit comments

Comments
 (0)