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. 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 golang#56899
  • Loading branch information
nwnt authored and Nont Thanonchai committed Jan 26, 2023
1 parent 707f888 commit 152a736
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 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,10 @@ func LoadGetFinalPathNameByHandle() error {
return procGetFinalPathNameByHandleW.Find()
}

func ErrorLoadingGetTempPath2() error {
return procGetTempPath2W.Find()
}

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

15 changes: 14 additions & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"internal/poll"
"internal/syscall/windows"
"runtime"
"sync"
"syscall"
"unicode/utf16"
"unsafe"
Expand Down Expand Up @@ -299,11 +300,23 @@ func Pipe() (r *File, w *File, err error) {
return newFile(p[0], "|0", "pipe"), newFile(p[1], "|1", "pipe"), nil
}

var (
useGetTempPath2Once sync.Once
useGetTempPath2 bool
)

func tempDir() string {
useGetTempPath2Once.Do(func() {
useGetTempPath2 = (windows.ErrorLoadingGetTempPath2() == nil)
})
getTempPath := syscall.GetTempPath
if useGetTempPath2 {
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 152a736

Please sign in to comment.