forked from keybase/client
/
ready.go
65 lines (58 loc) · 1.97 KB
/
ready.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
// 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 data
import (
"context"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/libkey"
"github.com/keybase/client/go/protocol/keybase1"
)
// ReadyBlock is a thin wrapper around ReadyProvider.Ready() that
// handles checking for duplicates.
func ReadyBlock(
ctx context.Context, bcache BlockCache, rp ReadyProvider,
kmd libkey.KeyMetadata, block Block, chargedTo keybase1.UserOrTeamID,
bType keybase1.BlockType, hashBehavior BlockCacheHashBehavior) (
info BlockInfo, plainSize int, readyBlockData ReadyBlockData, err error) {
var ptr BlockPointer
directType := DirectBlock
if block.IsIndirect() {
directType = IndirectBlock
} else if fBlock, ok := block.(*FileBlock); ok {
// first see if we are duplicating any known blocks in this folder
ptr, err = bcache.CheckForKnownPtr(kmd.TlfID(), fBlock, hashBehavior)
if err != nil {
return BlockInfo{}, 0, ReadyBlockData{}, err
}
}
// Ready the block, even in the case where we can reuse an
// existing block, just so that we know what the size of the
// encrypted data will be.
bid, plainSize, readyBlockData, err := rp.Ready(ctx, kmd, block)
if err != nil {
return BlockInfo{}, 0, ReadyBlockData{}, err
}
if ptr.IsInitialized() {
ptr.RefNonce, err = kbfsblock.MakeRefNonce()
if err != nil {
return BlockInfo{}, 0, ReadyBlockData{}, err
}
ptr.SetWriter(chargedTo)
// In case we're deduping an old pointer with an unknown block type.
ptr.DirectType = directType
} else {
ptr = BlockPointer{
ID: bid,
KeyGen: kmd.LatestKeyGeneration(),
DataVer: block.DataVersion(),
DirectType: directType,
Context: kbfsblock.MakeFirstContext(chargedTo, bType),
}
}
info = BlockInfo{
BlockPointer: ptr,
EncodedSize: uint32(readyBlockData.GetEncodedSize()),
}
return info, plainSize, readyBlockData, nil
}