Source file
src/syscall/pwd_plan9.go
1
2
3
4
5
6
7
8
9
10
11
12 package syscall
13
14 import "sync"
15
16 var (
17 wdmu sync.Mutex
18 wdSet bool
19 wdStr string
20 )
21
22 func Fixwd() {
23 wdmu.Lock()
24 defer wdmu.Unlock()
25 fixwdLocked()
26 }
27
28 func fixwdLocked() {
29 if !wdSet {
30 return
31 }
32
33 wd, _ := getwd()
34 if wd == wdStr {
35 return
36 }
37 if err := chdir(wdStr); err != nil {
38 return
39 }
40 }
41
42 func fixwd(paths ...string) {
43 for _, path := range paths {
44 if path != "" && path[0] != '/' && path[0] != '#' {
45 Fixwd()
46 return
47 }
48 }
49 }
50
51
52 func getwd() (wd string, err error) {
53 fd, err := open(".", O_RDONLY)
54 if err != nil {
55 return "", err
56 }
57 defer Close(fd)
58 return Fd2path(fd)
59 }
60
61 func Getwd() (wd string, err error) {
62 wdmu.Lock()
63 defer wdmu.Unlock()
64
65 if wdSet {
66 return wdStr, nil
67 }
68 wd, err = getwd()
69 if err != nil {
70 return
71 }
72 wdSet = true
73 wdStr = wd
74 return wd, nil
75 }
76
77 func Chdir(path string) error {
78 fixwd(path)
79 wdmu.Lock()
80 defer wdmu.Unlock()
81
82 if err := chdir(path); err != nil {
83 return err
84 }
85
86 wd, err := getwd()
87 if err != nil {
88 return err
89 }
90 wdSet = true
91 wdStr = wd
92 return nil
93 }
94
View as plain text