Commit ca471573 authored by Alex Brainman's avatar Alex Brainman

os: change Open(`C:`) to open current directory on C:

Open(`C:`) currently opens root directory on C:. Change that to open
current directory on C:. Just like cmd.exe's "dir C:" command does.
Just like FindFirstFile("C:*") Windows API does. It is also consistent
with what filepath.Join("C:", "a") currently does.

Fixes #13763

Change-Id: I60b6e7d80215d110bbbb6265c9f32717401638c6
Reviewed-on: https://go-review.googlesource.com/18184Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
parent e35901fd
......@@ -90,7 +90,13 @@ func openFile(name string, flag int, perm FileMode) (file *File, err error) {
}
func openDir(name string) (file *File, err error) {
maskp, e := syscall.UTF16PtrFromString(name + `\*`)
var mask string
if len(name) == 2 && name[1] == ':' { // it is a drive letter, like C:
mask = name + `*`
} else {
mask = name + `\*`
}
maskp, e := syscall.UTF16PtrFromString(mask)
if e != nil {
return nil, e
}
......
......@@ -9,6 +9,7 @@ import (
"os"
osexec "os/exec"
"path/filepath"
"sort"
"strings"
"syscall"
"testing"
......@@ -179,3 +180,46 @@ func TestStatDir(t *testing.T) {
t.Fatal("race condition occured")
}
}
func TestOpenVolumeName(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "TestOpenVolumeName")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(tmpdir)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
want := []string{"file1", "file2", "file3", "gopher.txt"}
sort.Strings(want)
for _, name := range want {
err := ioutil.WriteFile(filepath.Join(tmpdir, name), nil, 0777)
if err != nil {
t.Fatal(err)
}
}
f, err := os.Open(filepath.VolumeName(tmpdir))
if err != nil {
t.Fatal(err)
}
defer f.Close()
have, err := f.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}
sort.Strings(have)
if strings.Join(want, "/") != strings.Join(have, "/") {
t.Fatalf("unexpected file list %q, want %q", have, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment