github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/dir_block_map_disk.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 libkbfs
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/libkey"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // dirBlockMapDisk tracks dir block info while making a revision, by
    16  // using a disk-based block cache.
    17  type dirBlockMapDisk struct {
    18  	dirtyBcache *DirtyBlockCacheDisk
    19  	kmd         libkey.KeyMetadata
    20  	ptrs        map[data.BlockPointer]bool
    21  }
    22  
    23  var _ dirBlockMap = (*dirBlockMapDisk)(nil)
    24  
    25  func newDirBlockMapDisk(
    26  	dirtyBcache *DirtyBlockCacheDisk, kmd libkey.KeyMetadata) *dirBlockMapDisk {
    27  	return &dirBlockMapDisk{
    28  		dirtyBcache: dirtyBcache,
    29  		kmd:         kmd,
    30  		ptrs:        make(map[data.BlockPointer]bool),
    31  	}
    32  }
    33  
    34  func (dbmd *dirBlockMapDisk) putBlock(
    35  	ctx context.Context, ptr data.BlockPointer, block *data.DirBlock) error {
    36  	err := dbmd.dirtyBcache.Put(
    37  		ctx, dbmd.kmd.TlfID(), ptr, data.MasterBranch, block)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	dbmd.ptrs[ptr] = true
    43  	return nil
    44  }
    45  
    46  func (dbmd *dirBlockMapDisk) getBlock(
    47  	ctx context.Context, ptr data.BlockPointer) (*data.DirBlock, error) {
    48  	if !dbmd.ptrs[ptr] {
    49  		return nil, errors.Errorf("No such block %s", ptr)
    50  	}
    51  	block, err := dbmd.dirtyBcache.Get(ctx, dbmd.kmd.TlfID(), ptr, data.MasterBranch)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	dblock, ok := block.(*data.DirBlock)
    56  	if !ok {
    57  		return nil, errors.Errorf(
    58  			"Unexpected block type for dir block: %T", block)
    59  	}
    60  	return dblock, nil
    61  }
    62  
    63  func (dbmd *dirBlockMapDisk) hasBlock(
    64  	_ context.Context, ptr data.BlockPointer) (bool, error) {
    65  	return dbmd.ptrs[ptr], nil
    66  }
    67  
    68  func (dbmd *dirBlockMapDisk) deleteBlock(
    69  	_ context.Context, ptr data.BlockPointer) error {
    70  	delete(dbmd.ptrs, ptr)
    71  	return nil
    72  }
    73  
    74  // numBlocks only tracks the blocks that have been put into the dirty
    75  // block cache since `dbdm` was constructed.
    76  func (dbmd *dirBlockMapDisk) numBlocks() int {
    77  	return len(dbmd.ptrs)
    78  }