1
0
mirror of https://github.com/golang/go synced 2024-11-19 09:44:46 -07:00

os: make SameFile handle paths like c:a.txt properly

Fixes #8490.

LGTM=r, rsc
R=golang-codereviews, rsc, bradfitz, r
CC=golang-codereviews
https://golang.org/cl/127740043
This commit is contained in:
Alex Brainman 2014-08-19 14:59:56 +10:00
parent e4f3db3852
commit 2de65cad54
4 changed files with 69 additions and 12 deletions

View File

@ -117,8 +117,10 @@ func openDir(name string) (file *File, err error) {
} }
d.path = name d.path = name
if !isAbs(d.path) { if !isAbs(d.path) {
cwd, _ := Getwd() d.path, e = syscall.FullPath(d.path)
d.path = cwd + `\` + d.path if e != nil {
return nil, e
}
} }
f := newFile(r, name) f := newFile(r, name)
f.dirinfo = d f.dirinfo = d

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
"testing"
) )
func init() { func init() {
@ -25,3 +26,56 @@ func init() {
supportsSymlinks = false supportsSymlinks = false
} }
} }
func TestSameWindowsFile(t *testing.T) {
temp, err := ioutil.TempDir("", "TestSameWindowsFile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(temp)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(temp)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
f, err := os.Create("a")
if err != nil {
t.Fatal(err)
}
f.Close()
ia1, err := os.Stat("a")
if err != nil {
t.Fatal(err)
}
path, err := filepath.Abs("a")
if err != nil {
t.Fatal(err)
}
ia2, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(ia1, ia2) {
t.Errorf("files should be same")
}
p := filepath.VolumeName(path) + filepath.Base(path)
if err != nil {
t.Fatal(err)
}
ia3, err := os.Stat(p)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(ia1, ia3) {
t.Errorf("files should be same")
}
}

View File

@ -87,8 +87,10 @@ func Lstat(name string) (fi FileInfo, err error) {
} }
fs.path = name fs.path = name
if !isAbs(fs.path) { if !isAbs(fs.path) {
cwd, _ := Getwd() fs.path, e = syscall.FullPath(fs.path)
fs.path = cwd + `\` + fs.path if e != nil {
return nil, e
}
} }
return fs, nil return fs, nil
} }

View File

@ -129,9 +129,8 @@ func SetNonblock(fd Handle, nonblocking bool) (err error) {
return nil return nil
} }
// getFullPath retrieves the full path of the specified file. // FullPath retrieves the full path of the specified file.
// Just a wrapper for Windows GetFullPathName api. func FullPath(name string) (path string, err error) {
func getFullPath(name string) (path string, err error) {
p, err := UTF16PtrFromString(name) p, err := UTF16PtrFromString(name)
if err != nil { if err != nil {
return "", err return "", err
@ -160,7 +159,7 @@ func isSlash(c uint8) bool {
} }
func normalizeDir(dir string) (name string, err error) { func normalizeDir(dir string) (name string, err error) {
ndir, err := getFullPath(dir) ndir, err := FullPath(dir)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -199,9 +198,9 @@ func joinExeDirAndFName(dir, p string) (name string, err error) {
return "", err return "", err
} }
if volToUpper(int(p[0])) == volToUpper(int(d[0])) { if volToUpper(int(p[0])) == volToUpper(int(d[0])) {
return getFullPath(d + "\\" + p[2:]) return FullPath(d + "\\" + p[2:])
} else { } else {
return getFullPath(p) return FullPath(p)
} }
} }
} else { } else {
@ -211,9 +210,9 @@ func joinExeDirAndFName(dir, p string) (name string, err error) {
return "", err return "", err
} }
if isSlash(p[0]) { if isSlash(p[0]) {
return getFullPath(d[:2] + p) return FullPath(d[:2] + p)
} else { } else {
return getFullPath(d + "\\" + p) return FullPath(d + "\\" + p)
} }
} }
// we shouldn't be here // we shouldn't be here