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 authored and Nont Thanonchai committed Jan 25, 2023
1 parent 707f888 commit eb3dacf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/internal/syscall/windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const (
//sys GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
//sys SetFileInformationByHandle(handle syscall.Handle, fileInformationClass uint32, buf uintptr, bufsize uint32) (err error) = kernel32.SetFileInformationByHandle
//sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
//sys GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W

const (
// flags for CreateToolhelp32Snapshot
Expand Down Expand Up @@ -363,6 +364,13 @@ func LoadGetFinalPathNameByHandle() error {
return procGetFinalPathNameByHandleW.Find()
}

func LoadGetTempPath2() bool {
if err := procGetTempPath2W.Find(); err != nil {
return false
}
return true
}

//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock

Expand Down
10 changes: 10 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.

14 changes: 13 additions & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"unsafe"
)

var (
tempPath2Exists bool
)

func init() {
tempPath2Exists = windows.LoadGetTempPath2()
}

// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
Expand Down Expand Up @@ -300,10 +308,14 @@ func Pipe() (r *File, w *File, err error) {
}

func tempDir() string {
getTempPath := syscall.GetTempPath
if tempPath2Exists {
getTempPath = windows.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

0 comments on commit eb3dacf

Please sign in to comment.