forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc.go
160 lines (130 loc) · 2.55 KB
/
rc.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"time"
)
type RC struct {
path string
mtime int64
allowPath string
}
func eachDir(path string) (paths []string) {
path, err := filepath.Abs(path)
if err != nil {
return
}
paths = []string{path}
if path == "/" {
return
}
for i := len(path) - 1; i >= 0; i-- {
if path[i] == os.PathSeparator {
path = path[:i]
if path == "" {
path = "/"
}
paths = append(paths, path)
}
}
return
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func fileMtime(path string) (int64, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return 0, err
}
return fileInfo.ModTime().Unix(), nil
}
func fileHash(path string) (string, error) {
fd, err := os.Open(path)
if err != nil {
return "", err
}
hasher := sha256.New()
io.Copy(hasher, fd)
num := hasher.Sum(nil)
return fmt.Sprintf("%x", num), nil
}
// Creates a file
func touch(path string) (err error) {
file, err := os.OpenFile(path, os.O_CREATE, 0644)
if err != nil {
return
}
file.Close()
t := time.Now()
return os.Chtimes(path, t, t)
}
func findUp(searchDir string, fileName string) (path string) {
for _, dir := range eachDir(searchDir) {
path = filepath.Join(dir, fileName)
if fileExists(path) {
return
}
}
return ""
}
func FindRC(wd string, allowDir string) *RC {
rcPath := findUp(wd, ".envrc")
if rcPath == "" {
return nil
}
return RCFromPath(rcPath, allowDir)
}
func RCFromPath(path string, allowDir string) *RC {
mtime, err := fileMtime(path)
if err != nil {
return nil
}
hash, err := fileHash(path)
if err != nil {
return nil
}
allowPath := filepath.Join(allowDir, hash)
allowMtime, _ := fileMtime(allowPath)
if allowMtime > mtime {
mtime = allowMtime
}
return &RC{path, mtime, allowPath}
}
func RCFromEnv(path string, mtime int64) *RC {
return &RC{path, mtime, ""}
}
func (self *RC) Allow() (err error) {
if self.allowPath == "" {
return fmt.Errorf("Cannot allow empty path")
}
if err = os.MkdirAll(filepath.Dir(self.allowPath), 0755); err != nil {
return
}
if err = touch(self.allowPath); err != nil {
return
}
self.mtime, err = fileMtime(self.allowPath)
return
}
func (self *RC) Deny() error {
return os.Remove(self.allowPath)
}
func (self *RC) Allowed() bool {
_, err := os.Stat(self.allowPath)
return err == nil
}
func (self *RC) RelTo(wd string) string {
x, err := filepath.Rel(wd, self.path)
if err != nil {
panic(err)
}
return x
}
func (self *RC) Touch() error {
return touch(self.path)
}