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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
package objects
import (
"bufio"
"bytes"
"code.laria.me/petrific/acl"
"errors"
"fmt"
"sort"
)
type TreeEntryType string
const (
TETFile TreeEntryType = "file"
TETDir TreeEntryType = "dir"
TETSymlink TreeEntryType = "symlink"
)
var treeEntryTypes = map[TreeEntryType]struct{}{
TETFile: {},
TETDir: {},
TETSymlink: {},
}
type TreeEntry interface {
Type() TreeEntryType
ACL() acl.ACL
User() string
Group() string
equalContent(TreeEntry) bool
toProperties() properties
}
func compareTreeEntries(a, b TreeEntry) bool {
return a.Type() == b.Type() && a.equalContent(b)
}
type TreeEntryBase struct {
acl acl.ACL
user, group string
}
func baseFromExec(exec bool) (base TreeEntryBase) {
if exec {
base.acl = acl.ACLFromUnixPerms(0755)
} else {
base.acl = acl.ACLFromUnixPerms(0644)
}
return
}
func (teb TreeEntryBase) ACL() acl.ACL {
return teb.acl
}
func (teb TreeEntryBase) User() string {
return teb.user
}
func (teb TreeEntryBase) Group() string {
return teb.group
}
func (teb TreeEntryBase) toProperties() properties {
props := properties{"acl": teb.acl.String()}
if teb.user != "" {
props["user"] = teb.user
}
if teb.group != "" {
props["group"] = teb.group
}
return props
}
func (a TreeEntryBase) equalContent(b TreeEntryBase) bool {
return a.acl.Equals(b.acl) && a.user == b.user && a.group == b.group
}
type TreeEntryFile struct {
TreeEntryBase
Ref ObjectId
}
func NewTreeEntryFile(ref ObjectId, exec bool) TreeEntryFile {
return TreeEntryFile{
TreeEntryBase: baseFromExec(exec),
Ref: ref,
}
}
func (tef TreeEntryFile) Type() TreeEntryType {
return TETFile
}
func (tef TreeEntryFile) toProperties() properties {
props := tef.TreeEntryBase.toProperties()
props["ref"] = tef.Ref.String()
return props
}
func (a TreeEntryFile) equalContent(_b TreeEntry) bool {
b, ok := _b.(TreeEntryFile)
return ok && a.TreeEntryBase.equalContent(b.TreeEntryBase) && a.Ref.Equals(b.Ref)
}
type TreeEntryDir struct {
TreeEntryBase
Ref ObjectId
}
func NewTreeEntryDir(ref ObjectId, exec bool) TreeEntryDir {
return TreeEntryDir{
TreeEntryBase: baseFromExec(exec),
Ref: ref,
}
}
func (ted TreeEntryDir) Type() TreeEntryType {
return TETDir
}
func (ted TreeEntryDir) toProperties() properties {
props := ted.TreeEntryBase.toProperties()
props["ref"] = ted.Ref.String()
return props
}
func (a TreeEntryDir) equalContent(_b TreeEntry) bool {
b, ok := _b.(TreeEntryDir)
return ok && a.TreeEntryBase.equalContent(b.TreeEntryBase) && a.Ref.Equals(b.Ref)
}
type TreeEntrySymlink struct {
TreeEntryBase
Target string
}
func NewTreeEntrySymlink(target string, exec bool) TreeEntrySymlink {
return TreeEntrySymlink{
TreeEntryBase: baseFromExec(exec),
Target: target,
}
}
func (tes TreeEntrySymlink) Type() TreeEntryType {
return TETSymlink
}
func (tes TreeEntrySymlink) toProperties() properties {
props := tes.TreeEntryBase.toProperties()
props["target"] = tes.Target
return props
}
func (a TreeEntrySymlink) equalContent(_b TreeEntry) bool {
b, ok := _b.(TreeEntrySymlink)
return ok && a.TreeEntryBase.equalContent(b.TreeEntryBase) && a.Target == b.Target
}
type Tree map[string]TreeEntry
func (t Tree) Type() ObjectType {
return OTTree
}
func (t Tree) Payload() (out []byte) {
lines := []string{}
for name, entry := range t {
props := entry.toProperties()
props["type"] = string(entry.Type())
props["name"] = name
line, err := props.MarshalText()
if err != nil {
panic(err)
}
lines = append(lines, string(line)+"\n")
}
sort.Strings(lines)
for _, line := range lines {
out = append(out, line...)
}
return
}
func getObjectIdFromProps(p properties, key string) (ObjectId, error) {
raw, ok := p[key]
if !ok {
return ObjectId{}, fmt.Errorf("Missing key: %s", key)
}
oid, err := ParseObjectId(raw)
return oid, err
}
func defaultFileTreeEntryBase(_acl *acl.ACL, props properties) (base TreeEntryBase) {
base.user = props["user"]
base.group = props["group"]
if _acl == nil {
base.acl = acl.ACLFromUnixPerms(0664)
} else {
base.acl = *_acl
}
return
}
func defaultDirTreeEntryBase(_acl *acl.ACL, props properties) (base TreeEntryBase) {
base.user = props["user"]
base.group = props["group"]
if _acl == nil {
base.acl = acl.ACLFromUnixPerms(0775)
} else {
base.acl = *_acl
}
return
}
func (t Tree) FromPayload(payload []byte) error {
sc := bufio.NewScanner(bytes.NewReader(payload))
for sc.Scan() {
line := sc.Bytes()
if len(line) == 0 {
continue
}
props := make(properties)
if err := props.UnmarshalText(line); err != nil {
return err
}
var _acl *acl.ACL
if acl_string, ok := props["acl"]; ok {
acltmp, err := acl.ParseACL(acl_string)
if err != nil {
return err
}
_acl = &acltmp
}
entry_type, ok := props["type"]
if !ok {
return errors.New("Missing property: type")
}
var entry TreeEntry
switch TreeEntryType(entry_type) {
case TETFile:
ref, err := getObjectIdFromProps(props, "ref")
if err != nil {
return err
}
entry = TreeEntryFile{
TreeEntryBase: defaultFileTreeEntryBase(_acl, props),
Ref: ref,
}
case TETDir:
ref, err := getObjectIdFromProps(props, "ref")
if err != nil {
return err
}
entry = TreeEntryDir{
TreeEntryBase: defaultDirTreeEntryBase(_acl, props),
Ref: ref,
}
case TETSymlink:
target, ok := props["target"]
if !ok {
return errors.New("Missing key: target")
}
entry = TreeEntrySymlink{
TreeEntryBase: defaultFileTreeEntryBase(_acl, props),
Target: target,
}
default:
// TODO: Or should we just ignore this entry? There might be more types in the future...
return fmt.Errorf("Unknown tree entry type: %s", entry_type)
}
name, ok := props["name"]
if !ok {
return errors.New("Missing property: name")
}
t[name] = entry
}
return sc.Err()
}
func (a Tree) Equals(b Tree) bool {
for k, va := range a {
vb, ok := b[k]
if !ok || va.Type() != vb.Type() || !va.equalContent(vb) {
return false
}
}
for k := range b {
_, ok := a[k]
if !ok {
return false
}
}
return true
}
|