Skip to content

Commit 9e097be

Browse files
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 1c65b69 commit 9e097be

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

src/internal/syscall/windows/syscall_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,4 @@ func LoadGetFinalPathNameByHandle() error {
367367
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
368368

369369
//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
370+
//sys RtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers

src/internal/syscall/windows/zsyscall_windows.go

Lines changed: 7 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,17 @@ func Pipe() (r *File, w *File, err error) {
300300
}
301301

302302
func tempDir() string {
303+
getTempPath := syscall.GetTempPath
304+
var maj, min, build uint32
305+
windows.RtlGetNtVersionNumbers(&maj, &min, &build)
306+
build &= 0xffff
307+
if build >= 20348 {
308+
getTempPath = syscall.GetTempPath2
309+
}
303310
n := uint32(syscall.MAX_PATH)
304311
for {
305312
b := make([]uint16, n)
306-
n, _ = syscall.GetTempPath(uint32(len(b)), &b[0])
313+
n, _ = getTempPath(uint32(len(b)), &b[0])
307314
if n > uint32(len(b)) {
308315
continue
309316
}

src/syscall/syscall_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func NewCallbackCDecl(fn any) uintptr {
237237
//sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
238238
//sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]
239239
//sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW
240+
//sys GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W
240241
//sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error)
241242
//sys GetFileType(filehandle Handle) (n uint32, err error)
242243
//sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW

src/syscall/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.

0 commit comments

Comments
 (0)