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.
|
2014-04-17 21:17:15 -06:00
|
|
|
func Getwd() (dir string, err error) {
|
2014-08-07 06:58:25 -06:00
|
|
|
// Clumsy but widespread kludge:
|
|
|
|
// if $PWD is set and matches ".", use it.
|
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
|
|
|
}
|
2014-04-17 21:17:15 -06:00
|
|
|
dir = Getenv("PWD")
|
|
|
|
if len(dir) > 0 && dir[0] == '/' {
|
|
|
|
d, err := Stat(dir)
|
2012-02-02 19:16:18 -07:00
|
|
|
if err == nil && SameFile(dot, d) {
|
2014-04-17 21:17:15 -06:00
|
|
|
return dir, nil
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 06:58:25 -06: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-02-14 12:21:09 -07:00
|
|
|
// Apply same kludge but to cached dir instead of $PWD.
|
|
|
|
getwdCache.Lock()
|
2014-04-17 21:17:15 -06:00
|
|
|
dir = getwdCache.dir
|
2013-02-14 12:21:09 -07:00
|
|
|
getwdCache.Unlock()
|
2014-04-17 21:17:15 -06:00
|
|
|
if len(dir) > 0 {
|
|
|
|
d, err := Stat(dir)
|
2013-02-14 12:21:09 -07:00
|
|
|
if err == nil && SameFile(dot, d) {
|
2014-04-17 21:17:15 -06:00
|
|
|
return dir, nil
|
2013-02-14 12:21:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2014-04-17 21:17:15 -06:00
|
|
|
// adds /name to the beginning of dir.
|
|
|
|
dir = ""
|
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) {
|
2014-04-17 21:17:15 -06:00
|
|
|
dir = "/" + name + dir
|
2009-12-15 16:40:16 -07:00
|
|
|
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()
|
2014-04-17 21:17:15 -06:00
|
|
|
getwdCache.dir = dir
|
2013-02-14 12:21:09 -07:00
|
|
|
getwdCache.Unlock()
|
|
|
|
|
2014-04-17 21:17:15 -06:00
|
|
|
return dir, nil
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|