/
dir_data.go
414 lines (363 loc) · 11.4 KB
/
dir_data.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
414
// Copyright 2018 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 data
import (
"github.com/keybase/client/go/kbfs/idutil"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/libkey"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// dirBlockGetter is a function that gets a block suitable for
// reading or writing, and also returns whether the block was already
// dirty. It may be called from new goroutines, and must handle any
// required locks accordingly.
type dirBlockGetter func(context.Context, libkey.KeyMetadata, BlockPointer,
Path, BlockReqType) (dblock *DirBlock, wasDirty bool, err error)
// DirData is a helper struct for accessing and manipulating data
// within a directory. It's meant for use within a single scope, not
// for long-term storage. The caller must ensure goroutine-safety.
type DirData struct {
getter dirBlockGetter
tree *blockTree
}
// NewDirData creates a new DirData instance.
func NewDirData(
dir Path, chargedTo keybase1.UserOrTeamID, bsplit BlockSplitter,
kmd libkey.KeyMetadata, getter dirBlockGetter, cacher dirtyBlockCacher,
log logger.Logger, vlog *libkb.VDebugLog) *DirData {
dd := &DirData{
getter: getter,
}
dd.tree = &blockTree{
file: dir,
chargedTo: chargedTo,
kmd: kmd,
bsplit: bsplit,
getter: dd.blockGetter,
cacher: cacher,
log: log,
vlog: vlog,
}
return dd
}
func (dd *DirData) rootBlockPointer() BlockPointer {
return dd.tree.file.TailPointer()
}
func (dd *DirData) blockGetter(
ctx context.Context, kmd libkey.KeyMetadata, ptr BlockPointer,
dir Path, rtype BlockReqType) (
block BlockWithPtrs, wasDirty bool, err error) {
return dd.getter(ctx, kmd, ptr, dir, rtype)
}
var hiddenEntries = map[string]bool{
".kbfs_git": true,
".kbfs_autogit": true,
".kbfs_deleted_repos": true,
}
// GetTopBlock returns the top-most block in this directory block tree.
func (dd *DirData) GetTopBlock(ctx context.Context, rtype BlockReqType) (
*DirBlock, error) {
topBlock, _, err := dd.getter(
ctx, dd.tree.kmd, dd.rootBlockPointer(), dd.tree.file, rtype)
if err != nil {
return nil, err
}
return topBlock, nil
}
func (dd *DirData) obfuscator() Obfuscator {
return dd.tree.file.Obfuscator()
}
// GetChildren returns a map of all the child EntryInfos in this
// directory.
func (dd *DirData) GetChildren(ctx context.Context) (
children map[PathPartString]EntryInfo, err error) {
topBlock, err := dd.GetTopBlock(ctx, BlockRead)
if err != nil {
return nil, err
}
_, blocks, _, err := dd.tree.getBlocksForOffsetRange(
ctx, dd.rootBlockPointer(), topBlock, topBlock.FirstOffset(), nil,
false, true)
if err != nil {
return nil, err
}
numEntries := 0
for _, b := range blocks {
numEntries += len(b.(*DirBlock).Children)
}
children = make(map[PathPartString]EntryInfo, numEntries)
for _, b := range blocks {
for k, de := range b.(*DirBlock).Children {
if hiddenEntries[k] {
continue
}
children[NewPathPartString(k, dd.obfuscator())] = de.EntryInfo
}
}
return children, nil
}
// GetEntries returns a map of all the child DirEntrys in this
// directory.
func (dd *DirData) GetEntries(ctx context.Context) (
children map[PathPartString]DirEntry, err error) {
topBlock, err := dd.GetTopBlock(ctx, BlockRead)
if err != nil {
return nil, err
}
_, blocks, _, err := dd.tree.getBlocksForOffsetRange(
ctx, dd.rootBlockPointer(), topBlock, topBlock.FirstOffset(), nil,
false, true)
if err != nil {
return nil, err
}
numEntries := 0
for _, b := range blocks {
numEntries += len(b.(*DirBlock).Children)
}
children = make(map[PathPartString]DirEntry, numEntries)
for _, b := range blocks {
for k, de := range b.(*DirBlock).Children {
children[NewPathPartString(k, dd.obfuscator())] = de
}
}
return children, nil
}
// Lookup returns the DirEntry for the given entry named by `name` in
// this directory.
func (dd *DirData) Lookup(ctx context.Context, name PathPartString) (
DirEntry, error) {
topBlock, err := dd.GetTopBlock(ctx, BlockRead)
if err != nil {
return DirEntry{}, err
}
namePlain := name.Plaintext()
off := StringOffset(namePlain)
_, _, block, _, _, _, err := dd.tree.getBlockAtOffset(
ctx, topBlock, &off, BlockLookup)
if err != nil {
return DirEntry{}, err
}
de, ok := block.(*DirBlock).Children[namePlain]
if !ok {
return DirEntry{}, idutil.NoSuchNameError{Name: name.String()}
}
return de, nil
}
// createIndirectBlock creates a new indirect block and pick a new id
// for the existing block, and use the existing block's ID for the new
// indirect block that becomes the parent.
func (dd *DirData) createIndirectBlock(ctx context.Context, dver Ver) (
BlockWithPtrs, error) {
newID, err := kbfsblock.MakeTemporaryID()
if err != nil {
return nil, err
}
dblock := &DirBlock{
CommonBlock: CommonBlock{
IsInd: true,
},
IPtrs: []IndirectDirPtr{
{
BlockInfo: BlockInfo{
BlockPointer: BlockPointer{
ID: newID,
KeyGen: dd.tree.kmd.LatestKeyGeneration(),
DataVer: dver,
Context: kbfsblock.MakeFirstContext(
dd.tree.chargedTo,
dd.rootBlockPointer().GetBlockType()),
DirectType: dd.rootBlockPointer().DirectType,
},
EncodedSize: 0,
},
Off: "",
},
},
}
dd.tree.vlog.CLogf(
ctx, libkb.VLog1, "Creating new level of indirection for dir %v, "+
"new block id for old top level is %v",
dd.rootBlockPointer(), newID)
err = dd.tree.cacher(ctx, dd.rootBlockPointer(), dblock)
if err != nil {
return nil, err
}
return dblock, nil
}
func (dd *DirData) processModifiedBlock(
ctx context.Context, ptr BlockPointer,
parentBlocks []ParentBlockAndChildIndex, block *DirBlock) (
unrefs []BlockInfo, err error) {
newBlocks, newOffset := dd.tree.bsplit.SplitDirIfNeeded(block)
err = dd.tree.cacher(ctx, ptr, block)
if err != nil {
return nil, err
}
_, newUnrefs, err := dd.tree.markParentsDirty(ctx, parentBlocks)
if err != nil {
return nil, err
}
unrefs = append(unrefs, newUnrefs...)
if len(newBlocks) > 1 {
dd.tree.vlog.CLogf(
ctx, libkb.VLog1, "Making new right block for %v",
dd.rootBlockPointer())
rightParents, _, err := dd.tree.newRightBlock(
ctx, parentBlocks, newOffset, FirstValidVer,
NewDirBlockWithPtrs, dd.createIndirectBlock)
if err != nil {
return nil, err
}
if len(parentBlocks) == 0 {
// We just created the first level of indirection. In that
// case `newRightBlock` doesn't cache the old top block,
// so we should do it here.
err = dd.tree.cacher(
ctx, rightParents[0].pblock.(*DirBlock).IPtrs[0].BlockPointer,
newBlocks[0])
if err != nil {
return nil, err
}
}
// Cache the split block in place of the blank one made by
// `newRightBlock`.
pb := rightParents[len(rightParents)-1]
err = dd.tree.cacher(ctx, pb.childBlockPtr(), newBlocks[1])
if err != nil {
return nil, err
}
// Shift it over if needed.
_, newUnrefs, _, err := dd.tree.shiftBlocksToFillHole(ctx, rightParents)
if err != nil {
return nil, err
}
unrefs = append(unrefs, newUnrefs...)
}
return unrefs, nil
}
func (dd *DirData) addEntryHelper(
ctx context.Context, name PathPartString, newDe DirEntry,
errorIfExists, errorIfNoMatch bool) (
unrefs []BlockInfo, err error) {
topBlock, err := dd.GetTopBlock(ctx, BlockWrite)
if err != nil {
return nil, err
}
namePlain := name.Plaintext()
off := StringOffset(namePlain)
ptr, parentBlocks, block, _, _, _, err := dd.tree.getBlockAtOffset(
ctx, topBlock, &off, BlockWrite)
if err != nil {
return nil, err
}
dblock := block.(*DirBlock)
de, exists := dblock.Children[namePlain]
if errorIfExists && exists {
return nil, NameExistsError{name.String()}
} else if errorIfNoMatch &&
(!exists || de.BlockPointer != newDe.BlockPointer) {
return nil, idutil.NoSuchNameError{Name: name.String()}
}
dblock.Children[namePlain] = newDe
return dd.processModifiedBlock(ctx, ptr, parentBlocks, dblock)
}
// AddEntry adds a new entry to this directory.
func (dd *DirData) AddEntry(
ctx context.Context, newName PathPartString, newDe DirEntry) (
unrefs []BlockInfo, err error) {
return dd.addEntryHelper(ctx, newName, newDe, true, false)
}
// UpdateEntry updates an existing entry to this directory.
func (dd *DirData) UpdateEntry(
ctx context.Context, name PathPartString, newDe DirEntry) (
unrefs []BlockInfo, err error) {
return dd.addEntryHelper(ctx, name, newDe, false, true)
}
// SetEntry set an entry to this directory, whether it is new or existing.
func (dd *DirData) SetEntry(
ctx context.Context, name PathPartString, newDe DirEntry) (
unrefs []BlockInfo, err error) {
return dd.addEntryHelper(ctx, name, newDe, false, false)
}
// RemoveEntry removes an entry from this directory.
func (dd *DirData) RemoveEntry(ctx context.Context, name PathPartString) (
unrefs []BlockInfo, err error) {
topBlock, err := dd.GetTopBlock(ctx, BlockWrite)
if err != nil {
return nil, err
}
namePlain := name.Plaintext()
off := StringOffset(namePlain)
ptr, parentBlocks, block, _, _, _, err := dd.tree.getBlockAtOffset(
ctx, topBlock, &off, BlockWrite)
if err != nil {
return nil, err
}
dblock := block.(*DirBlock)
if _, exists := dblock.Children[namePlain]; !exists {
// Nothing to do.
return nil, nil
}
delete(dblock.Children, namePlain)
// For now, just leave the block empty, at its current place in
// the tree. TODO: remove empty blocks all the way up the tree
// and shift parent pointers around as needed.
return dd.processModifiedBlock(ctx, ptr, parentBlocks, dblock)
}
// Ready readies all the dirty child blocks for a directory tree with
// an indirect top-block, and updates their block IDs in their parent
// block's list of indirect pointers. It returns a map pointing from
// the new block info from any readied block to its corresponding old
// block pointer.
func (dd *DirData) Ready(ctx context.Context, id tlf.ID,
bcache BlockCache, dirtyBcache IsDirtyProvider,
rp ReadyProvider, bps BlockPutState,
topBlock *DirBlock, hashBehavior BlockCacheHashBehavior) (
map[BlockInfo]BlockPointer, error) {
return dd.tree.ready(
ctx, id, bcache, dirtyBcache, rp, bps, topBlock, nil, hashBehavior)
}
// GetDirtyChildPtrs returns a set of dirty child pointers (not the
// root pointer) for the directory.
func (dd *DirData) GetDirtyChildPtrs(
ctx context.Context, dirtyBcache IsDirtyProvider) (
ptrs map[BlockPointer]bool, err error) {
topBlock, err := dd.GetTopBlock(ctx, BlockRead)
if err != nil {
return nil, err
}
if !topBlock.IsIndirect() {
return nil, nil
}
ptrs = make(map[BlockPointer]bool)
// Gather all the paths to all dirty leaf blocks first.
off := topBlock.FirstOffset()
for off != nil {
_, parentBlocks, block, nextBlockOff, _, err :=
dd.tree.getNextDirtyBlockAtOffset(
ctx, topBlock, off, BlockWrite, dirtyBcache)
if err != nil {
return nil, err
}
if block == nil {
// No more dirty blocks.
break
}
off = nextBlockOff // Will be `nil` if there are no more blocks.
for _, pb := range parentBlocks {
ptrs[pb.childBlockPtr()] = true
}
}
return ptrs, nil
}
// GetIndirectDirBlockInfos returns all of the BlockInfos for blocks
// pointed to by indirect blocks within this directory tree.
func (dd *DirData) GetIndirectDirBlockInfos(ctx context.Context) (
[]BlockInfo, error) {
return dd.tree.getIndirectBlockInfos(ctx)
}