github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/data/ready.go (about)

     1  // Copyright 2019 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 data
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/keybase/client/go/kbfs/kbfsblock"
    11  	"github.com/keybase/client/go/kbfs/libkey"
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  )
    14  
    15  // ReadyBlock is a thin wrapper around ReadyProvider.Ready() that
    16  // handles checking for duplicates.
    17  func ReadyBlock(
    18  	ctx context.Context, bcache BlockCache, rp ReadyProvider,
    19  	kmd libkey.KeyMetadata, block Block, chargedTo keybase1.UserOrTeamID,
    20  	bType keybase1.BlockType, hashBehavior BlockCacheHashBehavior) (
    21  	info BlockInfo, plainSize int, readyBlockData ReadyBlockData, err error) {
    22  	var ptr BlockPointer
    23  	directType := DirectBlock
    24  	if block.IsIndirect() {
    25  		directType = IndirectBlock
    26  	} else if fBlock, ok := block.(*FileBlock); ok {
    27  		// first see if we are duplicating any known blocks in this folder
    28  		ptr, err = bcache.CheckForKnownPtr(kmd.TlfID(), fBlock, hashBehavior)
    29  		if err != nil {
    30  			return BlockInfo{}, 0, ReadyBlockData{}, err
    31  		}
    32  	}
    33  
    34  	// Ready the block, even in the case where we can reuse an
    35  	// existing block, just so that we know what the size of the
    36  	// encrypted data will be.
    37  	bid, plainSize, readyBlockData, err := rp.Ready(ctx, kmd, block)
    38  	if err != nil {
    39  		return BlockInfo{}, 0, ReadyBlockData{}, err
    40  	}
    41  
    42  	if ptr.IsInitialized() {
    43  		ptr.RefNonce, err = kbfsblock.MakeRefNonce()
    44  		if err != nil {
    45  			return BlockInfo{}, 0, ReadyBlockData{}, err
    46  		}
    47  		ptr.SetWriter(chargedTo)
    48  		// In case we're deduping an old pointer with an unknown block type.
    49  		ptr.DirectType = directType
    50  	} else {
    51  		ptr = BlockPointer{
    52  			ID:         bid,
    53  			KeyGen:     kmd.LatestKeyGeneration(),
    54  			DataVer:    block.DataVersion(),
    55  			DirectType: directType,
    56  			Context:    kbfsblock.MakeFirstContext(chargedTo, bType),
    57  		}
    58  	}
    59  
    60  	info = BlockInfo{
    61  		BlockPointer: ptr,
    62  		EncodedSize:  uint32(readyBlockData.GetEncodedSize()),
    63  	}
    64  	return info, plainSize, readyBlockData, nil
    65  }