github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/block_getter.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package libkbfs 6 7 import ( 8 "github.com/keybase/client/go/kbfs/data" 9 "github.com/keybase/client/go/kbfs/kbfscrypto" 10 "github.com/keybase/client/go/kbfs/libkey" 11 "golang.org/x/net/context" 12 ) 13 14 // blockGetter provides the API for the block retrieval worker to obtain blocks. 15 type blockGetter interface { 16 getBlock( 17 context.Context, libkey.KeyMetadata, data.BlockPointer, data.Block, 18 DiskBlockCacheType) error 19 assembleBlock( 20 context.Context, libkey.KeyMetadata, data.BlockPointer, data.Block, 21 []byte, kbfscrypto.BlockCryptKeyServerHalf) error 22 assembleBlockLocal( 23 context.Context, libkey.KeyMetadata, data.BlockPointer, data.Block, 24 []byte, kbfscrypto.BlockCryptKeyServerHalf) error 25 } 26 27 // realBlockGetter obtains real blocks using the APIs available in Config. 28 type realBlockGetter struct { 29 config blockOpsConfig 30 } 31 32 // getBlock implements the interface for realBlockGetter. 33 func (bg *realBlockGetter) getBlock( 34 ctx context.Context, kmd libkey.KeyMetadata, blockPtr data.BlockPointer, 35 block data.Block, cacheType DiskBlockCacheType) error { 36 bserv := bg.config.BlockServer() 37 buf, blockServerHalf, err := bserv.Get( 38 ctx, kmd.TlfID(), blockPtr.ID, blockPtr.Context, cacheType) 39 if err != nil { 40 return err 41 } 42 43 if _, isLocal := bserv.(blockServerLocal); isLocal { 44 return assembleBlockLocal( 45 ctx, bg.config.keyGetter(), bg.config.Codec(), 46 bg.config.cryptoPure(), kmd, blockPtr, block, buf, blockServerHalf) 47 } 48 return assembleBlock( 49 ctx, bg.config.keyGetter(), bg.config.Codec(), bg.config.cryptoPure(), 50 kmd, blockPtr, block, buf, blockServerHalf) 51 } 52 53 func (bg *realBlockGetter) assembleBlock(ctx context.Context, 54 kmd libkey.KeyMetadata, ptr data.BlockPointer, block data.Block, buf []byte, 55 serverHalf kbfscrypto.BlockCryptKeyServerHalf) error { 56 return assembleBlock(ctx, bg.config.keyGetter(), bg.config.Codec(), 57 bg.config.cryptoPure(), kmd, ptr, block, buf, serverHalf) 58 } 59 60 func (bg *realBlockGetter) assembleBlockLocal(ctx context.Context, 61 kmd libkey.KeyMetadata, ptr data.BlockPointer, block data.Block, buf []byte, 62 serverHalf kbfscrypto.BlockCryptKeyServerHalf) error { 63 return assembleBlockLocal(ctx, bg.config.keyGetter(), bg.config.Codec(), 64 bg.config.cryptoPure(), kmd, ptr, block, buf, serverHalf) 65 }