2009-05-18 11:49:34 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2013-02-14 12:21:09 -07:00
|
|
|
"sync"
|
2009-12-15 16:40:16 -07:00
|
|
|
"syscall"
|
2009-05-18 11:49:34 -06:00
|
|
|
)
|
|
|
|
|
2013-02-14 12:21:09 -07:00
|
|
|
var getwdCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2013-08-06 13:04:08 -06:00
|
|
|
// useSyscallwd determines whether to use the return value of
|
|
|
|
// syscall.Getwd based on its error.
|
|
|
|
var useSyscallwd = func(error) bool { return true }
|
|
|
|
|
2009-05-18 11:49:34 -06:00
|
|
|
// Getwd returns a rooted path name corresponding to the
|
|
|
|
// current directory. If the current directory can be
|
|
|
|
// reached via multiple paths (due to symbolic links),
|
|
|
|
// Getwd may return any one of them.
|
2011-11-30 10:04:16 -07:00
|
|
|
func Getwd() (pwd string, err error) {
|
2009-05-18 11:49:34 -06:00
|
|
|
// If the operating system provides a Getwd call, use it.
|
|
|
|
if syscall.ImplementsGetwd {
|
2009-12-15 16:40:16 -07:00
|
|
|
s, e := syscall.Getwd()
|
2013-08-06 13:04:08 -06:00
|
|
|
if useSyscallwd(e) {
|
2013-08-05 13:26:05 -06:00
|
|
|
return s, NewSyscallError("getwd", e)
|
|
|
|
}
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we're trying to find our way back to ".".
|
2009-12-15 16:40:16 -07:00
|
|
|
dot, err := Stat(".")
|
2009-05-18 11:49:34 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clumsy but widespread kludge:
|
|
|
|
// if $PWD is set and matches ".", use it.
|
2011-11-30 10:04:16 -07:00
|
|
|
pwd = Getenv("PWD")
|
2009-05-18 11:49:34 -06:00
|
|
|
if len(pwd) > 0 && pwd[0] == '/' {
|
2009-12-15 16:40:16 -07:00
|
|
|
d, err := Stat(pwd)
|
2012-02-02 19:16:18 -07:00
|
|
|
if err == nil && SameFile(dot, d) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return pwd, nil
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 12:21:09 -07:00
|
|
|
// Apply same kludge but to cached dir instead of $PWD.
|
|
|
|
getwdCache.Lock()
|
|
|
|
pwd = getwdCache.dir
|
|
|
|
getwdCache.Unlock()
|
|
|
|
if len(pwd) > 0 {
|
|
|
|
d, err := Stat(pwd)
|
|
|
|
if err == nil && SameFile(dot, d) {
|
|
|
|
return pwd, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 11:49:34 -06:00
|
|
|
// Root is a special case because it has no parent
|
|
|
|
// and ends in a slash.
|
2009-12-15 16:40:16 -07:00
|
|
|
root, err := Stat("/")
|
2009-05-18 11:49:34 -06:00
|
|
|
if err != nil {
|
|
|
|
// Can't stat root - no hope.
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
2012-02-02 19:16:18 -07:00
|
|
|
if SameFile(root, dot) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "/", nil
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// General algorithm: find name in parent
|
|
|
|
// and then find name of parent. Each iteration
|
|
|
|
// adds /name to the beginning of pwd.
|
2009-12-15 16:40:16 -07:00
|
|
|
pwd = ""
|
2009-11-09 22:23:52 -07:00
|
|
|
for parent := ".."; ; parent = "../" + parent {
|
2009-12-15 16:40:16 -07:00
|
|
|
if len(parent) >= 1024 { // Sanity check
|
2012-02-16 16:04:29 -07:00
|
|
|
return "", syscall.ENAMETOOLONG
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
2011-04-05 00:42:14 -06:00
|
|
|
fd, err := Open(parent)
|
2009-05-18 11:49:34 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2009-12-15 16:40:16 -07:00
|
|
|
names, err := fd.Readdirnames(100)
|
2009-05-18 11:49:34 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:40:16 -07:00
|
|
|
fd.Close()
|
|
|
|
return "", err
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, name := range names {
|
2009-12-15 16:40:16 -07:00
|
|
|
d, _ := Lstat(parent + "/" + name)
|
2012-02-02 19:16:18 -07:00
|
|
|
if SameFile(d, dot) {
|
2009-12-15 16:40:16 -07:00
|
|
|
pwd = "/" + name + pwd
|
|
|
|
goto Found
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Found:
|
2009-12-15 16:40:16 -07:00
|
|
|
pd, err := fd.Stat()
|
2009-05-18 11:49:34 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
fd.Close()
|
2012-02-02 19:16:18 -07:00
|
|
|
if SameFile(pd, root) {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
// Set up for next round.
|
2009-12-15 16:40:16 -07:00
|
|
|
dot = pd
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
2013-02-14 12:21:09 -07:00
|
|
|
|
|
|
|
// Save answer as hint to avoid the expensive path next time.
|
|
|
|
getwdCache.Lock()
|
|
|
|
getwdCache.dir = pwd
|
|
|
|
getwdCache.Unlock()
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
return pwd, nil
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|