aboutsummaryrefslogtreecommitdiff
path: root/objects/object_tree.go
blob: 2708aaa24d232368548ecc76097579e20206e82c (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
}

// Tree objects represent a filesystem tree / directory.
// It contains references to files (See `File`), symlinks and other trees plus their metadata.
// It is serialized as a sorted list of `Property` serializations (seperated by newline '\n').
// All entries have the property keys "name" and "type" (and optionally "user", "group" and "acl" representing a posix ACL.
// Currently only the execution bit is actually considerer. Choosing posix ACLs gives us the
// possibility to extend the privilege system later).
// Further keys depend on the value of type:
//
// type=file, type=dir =>
//
// ref: Holding the ID referencing a file / subtree
//
// type=symlink
//
// target: Holding the (relative) symlink path
//
// The Property format allows easy extension in the future while remaining compatible
// to older versions (they then simply ignore the additional properties).
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
}