Skip to content

Commit 49abcf1

Browse files
committed
os: adjust TempDir for Z:\
If TMP environment variable is set to Z:\, TempDir returns Z:. But Z: refers to current directory on Z:, while Z:\ refers to root directory on Z:. Adjust TempDir to return Z:\. Fixes #29291 Change-Id: If04d0c7977a8ac2d9d558307502e81beb68776ef Reviewed-on: https://go-review.googlesource.com/c/154384 Run-TryBot: Alex Brainman <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 7e1ec1e commit 49abcf1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/os/file_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ func tempDir() string {
325325
if n > uint32(len(b)) {
326326
continue
327327
}
328-
if n > 0 && b[n-1] == '\\' {
328+
if n == 3 && b[1] == ':' && b[2] == '\\' {
329+
// Do nothing for path, like C:\.
330+
} else if n > 0 && b[n-1] == '\\' {
331+
// Otherwise remove terminating \.
329332
n--
330333
}
331334
return string(utf16.Decode(b[:n]))

src/os/os_windows_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package os_test
66

77
import (
8+
"errors"
89
"fmt"
910
"internal/poll"
1011
"internal/syscall/windows"
@@ -1003,3 +1004,46 @@ func TestStatOfInvalidName(t *testing.T) {
10031004
t.Fatal(`os.Stat("*.go") unexpectedly succeeded`)
10041005
}
10051006
}
1007+
1008+
// findUnusedDriveLetter searches mounted drive list on the system
1009+
// (starting from Z: and ending at D:) for unused drive letter.
1010+
// It returns path to the found drive root directory (like Z:\) or error.
1011+
func findUnusedDriveLetter() (string, error) {
1012+
// Do not use A: and B:, because they are reserved for floppy drive.
1013+
// Do not use C:, becasue it is normally used for main drive.
1014+
for l := 'Z'; l >= 'D'; l-- {
1015+
p := string(l) + `:\`
1016+
_, err := os.Stat(p)
1017+
if os.IsNotExist(err) {
1018+
return p, nil
1019+
}
1020+
}
1021+
return "", errors.New("Could not find unused drive letter.")
1022+
}
1023+
1024+
func TestRootDirAsTemp(t *testing.T) {
1025+
testenv.MustHaveExec(t)
1026+
1027+
if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
1028+
fmt.Print(os.TempDir())
1029+
os.Exit(0)
1030+
}
1031+
1032+
newtmp, err := findUnusedDriveLetter()
1033+
if err != nil {
1034+
t.Fatal(err)
1035+
}
1036+
1037+
cmd := osexec.Command(os.Args[0], "-test.run=TestRootDirAsTemp")
1038+
cmd.Env = os.Environ()
1039+
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
1040+
cmd.Env = append(cmd.Env, "TMP="+newtmp)
1041+
cmd.Env = append(cmd.Env, "TEMP="+newtmp)
1042+
output, err := cmd.CombinedOutput()
1043+
if err != nil {
1044+
t.Fatalf("Failed to spawn child process: %v %q", err, string(output))
1045+
}
1046+
if want, have := newtmp, string(output); have != want {
1047+
t.Fatalf("unexpected child process output %q, want %q", have, want)
1048+
}
1049+
}

0 commit comments

Comments
 (0)