2010-12-03 05:34:57 +01: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 (
|
2015-01-15 01:27:56 +01:00
|
|
|
"runtime"
|
2013-07-16 08:54:42 +02:00
|
|
|
"sync"
|
2010-12-03 05:34:57 +01:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
var getwdCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// useSyscallwd determines whether to use the return value of
|
|
|
|
// syscall.Getwd based on its error.
|
|
|
|
var useSyscallwd = func(error) bool { return true }
|
|
|
|
|
2010-12-03 05:34:57 +01: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.
|
2014-07-19 10:53:52 +02:00
|
|
|
func Getwd() (dir string, err error) {
|
2015-01-15 01:27:56 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return syscall.Getwd()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// Clumsy but widespread kludge:
|
|
|
|
// if $PWD is set and matches ".", use it.
|
2010-12-03 05:34:57 +01:00
|
|
|
dot, err := Stat(".")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
dir = Getenv("PWD")
|
|
|
|
if len(dir) > 0 && dir[0] == '/' {
|
|
|
|
d, err := Stat(dir)
|
2012-02-09 09:19:58 +01:00
|
|
|
if err == nil && SameFile(dot, d) {
|
2014-07-19 10:53:52 +02:00
|
|
|
return dir, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Apply same kludge but to cached dir instead of $PWD.
|
|
|
|
getwdCache.Lock()
|
2014-07-19 10:53:52 +02:00
|
|
|
dir = getwdCache.dir
|
2013-07-16 08:54:42 +02:00
|
|
|
getwdCache.Unlock()
|
2014-07-19 10:53:52 +02:00
|
|
|
if len(dir) > 0 {
|
|
|
|
d, err := Stat(dir)
|
2013-07-16 08:54:42 +02:00
|
|
|
if err == nil && SameFile(dot, d) {
|
2014-07-19 10:53:52 +02:00
|
|
|
return dir, nil
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Root is a special case because it has no parent
|
|
|
|
// and ends in a slash.
|
|
|
|
root, err := Stat("/")
|
|
|
|
if err != nil {
|
|
|
|
// Can't stat root - no hope.
|
|
|
|
return "", err
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
if SameFile(root, dot) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return "/", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// General algorithm: find name in parent
|
|
|
|
// and then find name of parent. Each iteration
|
2014-07-19 10:53:52 +02:00
|
|
|
// adds /name to the beginning of dir.
|
|
|
|
dir = ""
|
2010-12-03 05:34:57 +01:00
|
|
|
for parent := ".."; ; parent = "../" + parent {
|
|
|
|
if len(parent) >= 1024 { // Sanity check
|
2012-03-02 21:01:37 +01:00
|
|
|
return "", syscall.ENAMETOOLONG
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err := Open(parent)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
names, err := fd.Readdirnames(100)
|
|
|
|
if err != nil {
|
|
|
|
fd.Close()
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
d, _ := Lstat(parent + "/" + name)
|
2012-02-09 09:19:58 +01:00
|
|
|
if SameFile(d, dot) {
|
2014-07-19 10:53:52 +02:00
|
|
|
dir = "/" + name + dir
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Found:
|
|
|
|
pd, err := fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fd.Close()
|
2012-02-09 09:19:58 +01:00
|
|
|
if SameFile(pd, root) {
|
2010-12-03 05:34:57 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Set up for next round.
|
|
|
|
dot = pd
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
// Save answer as hint to avoid the expensive path next time.
|
|
|
|
getwdCache.Lock()
|
2014-07-19 10:53:52 +02:00
|
|
|
getwdCache.dir = dir
|
2013-07-16 08:54:42 +02:00
|
|
|
getwdCache.Unlock()
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
return dir, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|