mirror of
https://github.com/golang/go
synced 2024-11-23 16:30:06 -07:00
os: in Getwd, $PWD override syscall.Getwd
This makes os.Getwd mimic C getwd on OS X, and possibly other systems. The change on OS X was a regression from 1.2 to 1.3. Fixes #8400. LGTM=bradfitz R=iant, bradfitz CC=golang-codereviews https://golang.org/cl/118970043
This commit is contained in:
parent
689995a73e
commit
6cee4d3e8f
@ -23,22 +23,12 @@ var useSyscallwd = func(error) bool { return true }
|
||||
// reached via multiple paths (due to symbolic links),
|
||||
// Getwd may return any one of them.
|
||||
func Getwd() (dir string, err error) {
|
||||
// If the operating system provides a Getwd call, use it.
|
||||
if syscall.ImplementsGetwd {
|
||||
s, e := syscall.Getwd()
|
||||
if useSyscallwd(e) {
|
||||
return s, NewSyscallError("getwd", e)
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, we're trying to find our way back to ".".
|
||||
// Clumsy but widespread kludge:
|
||||
// if $PWD is set and matches ".", use it.
|
||||
dot, err := Stat(".")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Clumsy but widespread kludge:
|
||||
// if $PWD is set and matches ".", use it.
|
||||
dir = Getenv("PWD")
|
||||
if len(dir) > 0 && dir[0] == '/' {
|
||||
d, err := Stat(dir)
|
||||
@ -47,6 +37,15 @@ func Getwd() (dir string, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// If the operating system provides a Getwd call, use it.
|
||||
// Otherwise, we're trying to find our way back to ".".
|
||||
if syscall.ImplementsGetwd {
|
||||
s, e := syscall.Getwd()
|
||||
if useSyscallwd(e) {
|
||||
return s, NewSyscallError("getwd", e)
|
||||
}
|
||||
}
|
||||
|
||||
// Apply same kludge but to cached dir instead of $PWD.
|
||||
getwdCache.Lock()
|
||||
dir = getwdCache.dir
|
||||
|
@ -811,8 +811,8 @@ func TestChdirAndGetwd(t *testing.T) {
|
||||
t.Fatalf("Open .: %s", err)
|
||||
}
|
||||
// These are chosen carefully not to be symlinks on a Mac
|
||||
// (unlike, say, /var, /etc, and /tmp).
|
||||
dirs := []string{"/", "/usr/bin"}
|
||||
// (unlike, say, /var, /etc), except /tmp, which we handle below.
|
||||
dirs := []string{"/", "/usr/bin", "/tmp"}
|
||||
// /usr/bin does not usually exist on Plan 9 or Android.
|
||||
switch runtime.GOOS {
|
||||
case "android":
|
||||
@ -820,6 +820,7 @@ func TestChdirAndGetwd(t *testing.T) {
|
||||
case "plan9":
|
||||
dirs = []string{"/", "/usr"}
|
||||
}
|
||||
oldwd := Getenv("PWD")
|
||||
for mode := 0; mode < 2; mode++ {
|
||||
for _, d := range dirs {
|
||||
if mode == 0 {
|
||||
@ -833,7 +834,11 @@ func TestChdirAndGetwd(t *testing.T) {
|
||||
err = fd1.Chdir()
|
||||
fd1.Close()
|
||||
}
|
||||
if d == "/tmp" {
|
||||
Setenv("PWD", "/tmp")
|
||||
}
|
||||
pwd, err1 := Getwd()
|
||||
Setenv("PWD", oldwd)
|
||||
err2 := fd.Chdir()
|
||||
if err2 != nil {
|
||||
// We changed the current directory and cannot go back.
|
||||
|
Loading…
Reference in New Issue
Block a user