Repository navigation
Expand file tree
/
Copy pathscripts.go
More file actions
218 lines (189 loc) · 5.17 KB
/
Copy pathscripts.go
File metadata and controls
218 lines (189 loc) · 5.17 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package scripts
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
"github.com/canonical/chisel/internal/fsutil"
)
type Value = starlark.Value
type RunOptions struct {
Label string
Namespace map[string]Value
Script string
}
func Run(opts *RunOptions) error {
thread := &starlark.Thread{Name: opts.Label}
fileOptions := &syntax.FileOptions{
TopLevelControl: true,
GlobalReassign: true,
}
globals, err := starlark.ExecFileOptions(fileOptions, thread, opts.Label, opts.Script, opts.Namespace)
_ = globals
return err
}
type ContentValue struct {
RootDir string
CheckRead func(path string) error
CheckWrite func(path string) error
// OnWrite has to be called after a successful write with the entry resulting
// from the write.
OnWrite func(entry *fsutil.Entry) error
}
// Content starlark.Value interface
// --------------------------------------------------------------------------
func (c *ContentValue) String() string {
return "Content{...}"
}
func (c *ContentValue) Type() string {
return "Content"
}
func (c *ContentValue) Freeze() {
}
func (c *ContentValue) Truth() starlark.Bool {
return true
}
func (c *ContentValue) Hash() (uint32, error) {
return starlark.String(c.RootDir).Hash()
}
// Content starlark.HasAttrs interface
// --------------------------------------------------------------------------
var _ starlark.HasAttrs = new(ContentValue)
func (c *ContentValue) Attr(name string) (Value, error) {
switch name {
case "read":
return starlark.NewBuiltin("Content.read", c.Read), nil
case "write":
return starlark.NewBuiltin("Content.write", c.Write), nil
case "list":
return starlark.NewBuiltin("Content.list", c.List), nil
}
return nil, nil
}
func (c *ContentValue) AttrNames() []string {
return []string{"read", "write", "list"}
}
// Content methods
// --------------------------------------------------------------------------
type Check uint
const (
CheckNone = 0
CheckRead = 1 << iota
CheckWrite
)
func (c *ContentValue) RealPath(path string, what Check) (string, error) {
if !filepath.IsAbs(c.RootDir) {
return "", fmt.Errorf("internal error: content defined with relative root: %s", c.RootDir)
}
if !filepath.IsAbs(path) {
return "", fmt.Errorf("content path must be absolute, got: %s", path)
}
cpath := filepath.Clean(path)
if cpath != "/" && strings.HasSuffix(path, "/") {
cpath += "/"
}
if c.CheckRead != nil && what&CheckRead != 0 {
err := c.CheckRead(cpath)
if err != nil {
return "", err
}
}
if c.CheckWrite != nil && what&CheckWrite != 0 {
err := c.CheckWrite(cpath)
if err != nil {
return "", err
}
}
rpath := filepath.Join(c.RootDir, cpath)
if lname, err := os.Readlink(rpath); err == nil {
lpath := filepath.Join(filepath.Dir(rpath), filepath.Clean(lname))
lrel, err := filepath.Rel(c.RootDir, lpath)
if err != nil || !filepath.IsAbs(lpath) {
return "", fmt.Errorf("invalid content symlink: %s", path)
}
return c.RealPath("/"+lrel, what)
}
return rpath, nil
}
func (c *ContentValue) polishError(path starlark.String, err error) error {
if e, ok := err.(*os.PathError); ok {
e.Path = path.GoString()
}
return err
}
func (c *ContentValue) Read(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (Value, error) {
var path starlark.String
err := starlark.UnpackArgs("Content.read", args, kwargs, "path", &path)
if err != nil {
return nil, err
}
fpath, err := c.RealPath(path.GoString(), CheckRead)
if err != nil {
return nil, err
}
data, err := os.ReadFile(fpath)
if err != nil {
return nil, c.polishError(path, err)
}
return starlark.String(data), nil
}
func (c *ContentValue) Write(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (Value, error) {
var path starlark.String
var data starlark.String
err := starlark.UnpackArgs("Content.write", args, kwargs, "path", &path, "data", &data)
if err != nil {
return nil, err
}
fpath, err := c.RealPath(path.GoString(), CheckWrite)
if err != nil {
return nil, err
}
fdata := []byte(data.GoString())
// No mode parameter for now as slices are supposed to list files
// explicitly instead.
entry, err := fsutil.Create(&fsutil.CreateOptions{
Root: "/",
Path: fpath,
Data: bytes.NewReader(fdata),
Mode: 0644,
})
if err != nil {
return nil, c.polishError(path, err)
}
err = c.OnWrite(entry)
if err != nil {
return nil, err
}
return starlark.None, nil
}
func (c *ContentValue) List(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (Value, error) {
var path starlark.String
err := starlark.UnpackArgs("Content.list", args, kwargs, "path", &path)
if err != nil {
return nil, err
}
dpath := path.GoString()
if !strings.HasSuffix(dpath, "/") {
dpath += "/"
}
fpath, err := c.RealPath(dpath, CheckRead)
if err != nil {
return nil, err
}
entries, err := os.ReadDir(fpath)
if err != nil {
return nil, c.polishError(path, err)
}
values := make([]Value, len(entries))
for i, entry := range entries {
name := entry.Name()
if entry.IsDir() {
name += "/"
}
values[i] = starlark.String(name)
}
return starlark.NewList(values), nil
}