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 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 golang#56899
  • Loading branch information
nwnt committed Jan 17, 2023
1 parent 1c65b69 commit 9e097be
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/internal/syscall/windows/syscall_windows.go
Expand Up @@ -367,3 +367,4 @@ func LoadGetFinalPathNameByHandle() error {
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock

//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
//sys RtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
7 changes: 7 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.

9 changes: 8 additions & 1 deletion src/os/file_windows.go
Expand Up @@ -300,10 +300,17 @@ func Pipe() (r *File, w *File, err error) {
}

func tempDir() string {
getTempPath := syscall.GetTempPath
var maj, min, build uint32
windows.RtlGetNtVersionNumbers(&maj, &min, &build)
build &= 0xffff
if build >= 20348 {
getTempPath = syscall.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
1 change: 1 addition & 0 deletions src/syscall/syscall_windows.go
Expand Up @@ -237,6 +237,7 @@ func NewCallbackCDecl(fn any) uintptr {
//sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
//sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]
//sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW
//sys GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W
//sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error)
//sys GetFileType(filehandle Handle) (n uint32, err error)
//sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW
Expand Down
10 changes: 10 additions & 0 deletions src/syscall/zsyscall_windows.go

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

0 comments on commit 9e097be

Please sign in to comment.