-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
node_wrappers.go
413 lines (359 loc) · 10.8 KB
/
node_wrappers.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2019 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libfs
import (
"context"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/kbfsmd"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
billy "gopkg.in/src-d/go-billy.v4"
)
type namedFileNode struct {
libkbfs.Node
log logger.Logger
name string
reader func(ctx context.Context) ([]byte, time.Time, error)
}
var _ libkbfs.Node = (*namedFileNode)(nil)
func (nfn *namedFileNode) GetFile(ctx context.Context) billy.File {
return &wrappedReadFile{
name: nfn.name,
reader: nfn.reader,
log: nfn.log,
}
}
func (nfn *namedFileNode) FillCacheDuration(d *time.Duration) {
// Suggest kindly that no one should cache this node, since it
// could change each time it's read.
*d = 0
}
func newFolderStatusFileNode(
config libkbfs.Config, node libkbfs.Node, fb data.FolderBranch,
log logger.Logger) *namedFileNode {
return &namedFileNode{
Node: node,
log: log,
name: StatusFileName,
reader: func(ctx context.Context) ([]byte, time.Time, error) {
return GetEncodedFolderStatus(ctx, config, fb)
},
}
}
func newMetricsFileNode(
config libkbfs.Config, node libkbfs.Node,
log logger.Logger) *namedFileNode {
return &namedFileNode{
Node: node,
log: log,
name: MetricsFileName,
reader: GetEncodedMetrics(config),
}
}
func newErrorFileNode(
config libkbfs.Config, node libkbfs.Node,
log logger.Logger) *namedFileNode {
return &namedFileNode{
Node: node,
log: log,
name: ErrorFileName,
reader: GetEncodedErrors(config),
}
}
func newTlfEditHistoryFileNode(
config libkbfs.Config, node libkbfs.Node, fb data.FolderBranch,
log logger.Logger) *namedFileNode {
return &namedFileNode{
Node: node,
log: log,
name: EditHistoryName,
reader: func(ctx context.Context) ([]byte, time.Time, error) {
return GetEncodedTlfEditHistory(ctx, config, fb)
},
}
}
func newUpdateHistoryFileNode(
config libkbfs.Config, node libkbfs.Node, fb data.FolderBranch,
start, end kbfsmd.Revision, log logger.Logger) *namedFileNode {
return &namedFileNode{
Node: node,
log: log,
name: UpdateHistoryFileName,
reader: func(ctx context.Context) ([]byte, time.Time, error) {
return GetEncodedUpdateHistory(ctx, config, fb, start, end)
},
}
}
var updateHistoryRevsRE = regexp.MustCompile("^\\.([0-9]+)(-([0-9]+))?$") //nolint (`\.` doesn't seem to work in single quotes)
type profileNode struct {
libkbfs.Node
config libkbfs.Config
name string
}
var _ libkbfs.Node = (*profileNode)(nil)
func (pn *profileNode) GetFile(ctx context.Context) billy.File {
fs := NewProfileFS(pn.config)
f, err := fs.Open(pn.name)
if err != nil {
return nil
}
return f
}
func (pn *profileNode) FillCacheDuration(d *time.Duration) {
// Suggest kindly that no one should cache this node, since it
// could change each time it's read.
*d = 0
}
type profileListNode struct {
libkbfs.Node
config libkbfs.Config
}
var _ libkbfs.Node = (*profileListNode)(nil)
func (pln *profileListNode) ShouldCreateMissedLookup(
ctx context.Context, name data.PathPartString) (
bool, context.Context, data.EntryType, os.FileInfo, data.PathPartString,
data.BlockPointer) {
namePlain := name.Plaintext()
fs := NewProfileFS(pln.config)
fi, err := fs.Lstat(namePlain)
if err != nil {
return pln.Node.ShouldCreateMissedLookup(ctx, name)
}
return true, ctx, data.FakeFile, fi, data.PathPartString{}, data.ZeroPtr
}
func (pln *profileListNode) WrapChild(child libkbfs.Node) libkbfs.Node {
child = pln.Node.WrapChild(child)
return &profileNode{child, pln.config, child.GetBasename().Plaintext()}
}
func (pln *profileListNode) GetFS(ctx context.Context) libkbfs.NodeFSReadOnly {
return NewProfileFS(pln.config)
}
// specialFileNode is a Node wrapper around a TLF node, that causes
// special files to be fake-created when they are accessed.
type specialFileNode struct {
libkbfs.Node
config libkbfs.Config
log logger.Logger
}
var _ libkbfs.Node = (*specialFileNode)(nil)
var perTlfWrappedNodeNames = map[string]bool{
StatusFileName: true,
UpdateHistoryFileName: true,
ProfileListDirName: true,
MetricsFileName: true,
ErrorFileName: true,
EditHistoryName: true,
}
var perTlfWrappedNodePrefixes = []string{
UpdateHistoryFileName,
DirBlockPrefix,
}
func shouldBeTlfWrappedNode(name string) bool {
for _, p := range perTlfWrappedNodePrefixes {
if strings.HasPrefix(name, p) {
return true
}
}
return perTlfWrappedNodeNames[name]
}
func (sfn *specialFileNode) newUpdateHistoryFileNode(
node libkbfs.Node, name string) *namedFileNode {
revs := strings.TrimPrefix(name, UpdateHistoryFileName)
if revs == "" {
return newUpdateHistoryFileNode(
sfn.config, node, sfn.GetFolderBranch(),
kbfsmd.RevisionInitial, kbfsmd.RevisionUninitialized, sfn.log)
}
matches := updateHistoryRevsRE.FindStringSubmatch(revs)
if len(matches) != 4 {
return nil
}
start, err := strconv.ParseUint(matches[1], 10, 64)
if err != nil {
return nil
}
end := start
if matches[3] != "" {
end, err = strconv.ParseUint(matches[3], 10, 64)
if err != nil {
return nil
}
}
return newUpdateHistoryFileNode(
sfn.config, node, sfn.GetFolderBranch(),
kbfsmd.Revision(start), kbfsmd.Revision(end), sfn.log)
}
// parseBlockPointer returns a real BlockPointer given a string. The
// format for the string is: id.keyGen.dataVer.creatorUID.directType
func parseBlockPointer(plain string) (data.BlockPointer, error) {
s := strings.Split(plain, ".")
if len(s) != 5 {
return data.ZeroPtr, errors.Errorf(
"%s is not in the right format for a block pointer", plain)
}
id, err := kbfsblock.IDFromString(s[0])
if err != nil {
return data.ZeroPtr, err
}
keyGen, err := strconv.Atoi(s[1])
if err != nil {
return data.ZeroPtr, err
}
dataVer, err := strconv.Atoi(s[2])
if err != nil {
return data.ZeroPtr, err
}
creator, err := keybase1.UserOrTeamIDFromString(s[3])
if err != nil {
return data.ZeroPtr, err
}
directType := data.BlockDirectTypeFromString(s[4])
return data.BlockPointer{
ID: id,
KeyGen: kbfsmd.KeyGen(keyGen),
DataVer: data.Ver(dataVer),
DirectType: directType,
Context: kbfsblock.MakeFirstContext(
creator, keybase1.BlockType_DATA),
}, nil
}
// ShouldCreateMissedLookup implements the Node interface for
// specialFileNode.
func (sfn *specialFileNode) ShouldCreateMissedLookup(
ctx context.Context, name data.PathPartString) (
bool, context.Context, data.EntryType, os.FileInfo, data.PathPartString,
data.BlockPointer) {
plain := name.Plaintext()
if !shouldBeTlfWrappedNode(plain) {
return sfn.Node.ShouldCreateMissedLookup(ctx, name)
}
switch {
case plain == StatusFileName:
sfn := newFolderStatusFileNode(
sfn.config, nil, sfn.GetFolderBranch(), sfn.log)
f := sfn.GetFile(ctx)
return true, ctx, data.FakeFile, f.(*wrappedReadFile).GetInfo(),
data.PathPartString{}, data.ZeroPtr
case plain == MetricsFileName:
mfn := newMetricsFileNode(sfn.config, nil, sfn.log)
f := mfn.GetFile(ctx)
return true, ctx, data.FakeFile, f.(*wrappedReadFile).GetInfo(),
data.PathPartString{}, data.ZeroPtr
case plain == ErrorFileName:
efn := newErrorFileNode(sfn.config, nil, sfn.log)
f := efn.GetFile(ctx)
return true, ctx, data.FakeFile, f.(*wrappedReadFile).GetInfo(),
data.PathPartString{}, data.ZeroPtr
case plain == EditHistoryName:
tehfn := newTlfEditHistoryFileNode(
sfn.config, nil, sfn.GetFolderBranch(), sfn.log)
f := tehfn.GetFile(ctx)
return true, ctx, data.FakeFile, f.(*wrappedReadFile).GetInfo(),
data.PathPartString{}, data.ZeroPtr
case plain == ProfileListDirName:
return true, ctx, data.FakeDir,
&wrappedReadFileInfo{plain, 0, sfn.config.Clock().Now(), true},
data.PathPartString{}, data.ZeroPtr
case strings.HasPrefix(plain, UpdateHistoryFileName):
uhfn := sfn.newUpdateHistoryFileNode(nil, plain)
if uhfn == nil {
return sfn.Node.ShouldCreateMissedLookup(ctx, name)
}
f := uhfn.GetFile(ctx)
return true, ctx, data.FakeFile, f.(*wrappedReadFile).GetInfo(),
data.PathPartString{}, data.ZeroPtr
case strings.HasPrefix(plain, DirBlockPrefix):
ptr, err := parseBlockPointer(strings.TrimPrefix(plain, DirBlockPrefix))
if err != nil {
sfn.log.CDebugf(
ctx, "Couldn't parse block pointer for %s: %+v", name, err)
return sfn.Node.ShouldCreateMissedLookup(ctx, name)
}
info := &wrappedReadFileInfo{
name: plain,
size: 0,
mtime: time.Now(),
dir: true,
}
return true, ctx, data.RealDir, info, data.PathPartString{}, ptr
default:
panic(fmt.Sprintf("Name %s was in map, but not in switch", name))
}
}
// WrapChild implements the Node interface for specialFileNode.
func (sfn *specialFileNode) WrapChild(child libkbfs.Node) libkbfs.Node {
child = sfn.Node.WrapChild(child)
name := child.GetBasename().Plaintext()
if !shouldBeTlfWrappedNode(name) {
if child.EntryType() == data.Dir {
// Wrap this child too, so we can look up special files in
// subdirectories of this node as well.
return &specialFileNode{
Node: child,
config: sfn.config,
log: sfn.log,
}
}
return child
}
switch {
case name == StatusFileName:
return newFolderStatusFileNode(
sfn.config, &libkbfs.ReadonlyNode{Node: child},
sfn.GetFolderBranch(), sfn.log)
case name == MetricsFileName:
return newMetricsFileNode(
sfn.config, &libkbfs.ReadonlyNode{Node: child}, sfn.log)
case name == ErrorFileName:
return newErrorFileNode(
sfn.config, &libkbfs.ReadonlyNode{Node: child}, sfn.log)
case name == EditHistoryName:
return newTlfEditHistoryFileNode(
sfn.config, &libkbfs.ReadonlyNode{Node: child},
sfn.GetFolderBranch(), sfn.log)
case name == ProfileListDirName:
return &profileListNode{
Node: &libkbfs.ReadonlyNode{Node: child},
config: sfn.config,
}
case strings.HasPrefix(name, UpdateHistoryFileName):
uhfn := sfn.newUpdateHistoryFileNode(child, name)
if uhfn == nil {
return child
}
return uhfn
case strings.HasPrefix(name, DirBlockPrefix):
return &libkbfs.ReadonlyNode{Node: child}
default:
panic(fmt.Sprintf("Name %s was in map, but not in switch", name))
}
}
// rootWrapper is a struct that manages wrapping root nodes with
// special per-TLF content.
type rootWrapper struct {
config libkbfs.Config
log logger.Logger
}
func (rw rootWrapper) wrap(node libkbfs.Node) libkbfs.Node {
return &specialFileNode{
Node: node,
config: rw.config,
log: rw.log,
}
}
// AddRootWrapper should be called on startup by any KBFS interface
// that wants to handle special files.
func AddRootWrapper(config libkbfs.Config) {
rw := rootWrapper{config, config.MakeLogger("")}
config.AddRootNodeWrapper(rw.wrap)
}