summaryrefslogtreecommitdiff
path: root/libgo/go/os/getwd.go
blob: 8c5ff7fca51a53ad64ab7e202e23d3061d095b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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 (
	"sync"
	"syscall"
)

var getwdCache struct {
	sync.Mutex
	dir string
}

// useSyscallwd determines whether to use the return value of
// syscall.Getwd based on its error.
var useSyscallwd = func(error) bool { return true }

// 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.
func Getwd() (pwd 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 ".".
	dot, err := Stat(".")
	if err != nil {
		return "", err
	}

	// Clumsy but widespread kludge:
	// if $PWD is set and matches ".", use it.
	pwd = Getenv("PWD")
	if len(pwd) > 0 && pwd[0] == '/' {
		d, err := Stat(pwd)
		if err == nil && SameFile(dot, d) {
			return pwd, nil
		}
	}

	// 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
		}
	}

	// 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
	}
	if SameFile(root, dot) {
		return "/", nil
	}

	// General algorithm: find name in parent
	// and then find name of parent.  Each iteration
	// adds /name to the beginning of pwd.
	pwd = ""
	for parent := ".."; ; parent = "../" + parent {
		if len(parent) >= 1024 { // Sanity check
			return "", syscall.ENAMETOOLONG
		}
		fd, err := Open(parent)
		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)
				if SameFile(d, dot) {
					pwd = "/" + name + pwd
					goto Found
				}
			}
		}

	Found:
		pd, err := fd.Stat()
		if err != nil {
			return "", err
		}
		fd.Close()
		if SameFile(pd, root) {
			break
		}
		// Set up for next round.
		dot = pd
	}

	// Save answer as hint to avoid the expensive path next time.
	getwdCache.Lock()
	getwdCache.dir = pwd
	getwdCache.Unlock()

	return pwd, nil
}