diff options
Diffstat (limited to 'lib/btrfs/btrfstree')
-rw-r--r-- | lib/btrfs/btrfstree/node_exp.go | 63 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/types_node.go | 49 |
2 files changed, 63 insertions, 49 deletions
diff --git a/lib/btrfs/btrfstree/node_exp.go b/lib/btrfs/btrfstree/node_exp.go new file mode 100644 index 0000000..5311ea0 --- /dev/null +++ b/lib/btrfs/btrfstree/node_exp.go @@ -0,0 +1,63 @@ +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package btrfstree + +import ( + "fmt" + + "github.com/datawire/dlib/derror" + + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim" + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" + "git.lukeshu.com/btrfs-progs-ng/lib/containers" +) + +type NodeExpectations struct { + LAddr containers.Optional[btrfsvol.LogicalAddr] + // Things knowable from the parent. + Level containers.Optional[uint8] + Generation containers.Optional[btrfsprim.Generation] + Owner func(btrfsprim.ObjID, btrfsprim.Generation) error + MinItem containers.Optional[btrfsprim.Key] + // Things knowable from the structure of the tree. + MaxItem containers.Optional[btrfsprim.Key] +} + +func (exp NodeExpectations) Check(node *Node) error { + var errs derror.MultiError + if exp.LAddr.OK && node.Head.Addr != exp.LAddr.Val { + errs = append(errs, fmt.Errorf("read from laddr=%v but claims to be at laddr=%v", + exp.LAddr.Val, node.Head.Addr)) + } + if exp.Level.OK && node.Head.Level != exp.Level.Val { + errs = append(errs, fmt.Errorf("expected level=%v but claims to be level=%v", + exp.Level.Val, node.Head.Level)) + } + if exp.Generation.OK && node.Head.Generation != exp.Generation.Val { + errs = append(errs, fmt.Errorf("expected generation=%v but claims to be generation=%v", + exp.Generation.Val, node.Head.Generation)) + } + if exp.Owner != nil { + if err := exp.Owner(node.Head.Owner, node.Head.Generation); err != nil { + errs = append(errs, err) + } + } + if node.Head.NumItems == 0 { + errs = append(errs, fmt.Errorf("has no items")) + } else { + if minItem, _ := node.MinItem(); exp.MinItem.OK && exp.MinItem.Val.Compare(minItem) > 0 { + errs = append(errs, fmt.Errorf("expected minItem>=%v but node has minItem=%v", + exp.MinItem, minItem)) + } + if maxItem, _ := node.MaxItem(); exp.MaxItem.OK && exp.MaxItem.Val.Compare(maxItem) < 0 { + errs = append(errs, fmt.Errorf("expected maxItem<=%v but node has maxItem=%v", + exp.MaxItem, maxItem)) + } + } + if len(errs) > 0 { + return errs + } + return nil +} diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go index bfcbbf4..ed9ba58 100644 --- a/lib/btrfs/btrfstree/types_node.go +++ b/lib/btrfs/btrfstree/types_node.go @@ -10,7 +10,6 @@ import ( "fmt" "git.lukeshu.com/go/typedsync" - "github.com/datawire/dlib/derror" "git.lukeshu.com/btrfs-progs-ng/lib/binstruct" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem" @@ -412,17 +411,6 @@ func (node *Node) LeafFreeSpace() uint32 { var ErrNotANode = errors.New("does not look like a node") -type NodeExpectations struct { - LAddr containers.Optional[btrfsvol.LogicalAddr] - // Things knowable from the parent. - Level containers.Optional[uint8] - Generation containers.Optional[btrfsprim.Generation] - Owner func(btrfsprim.ObjID, btrfsprim.Generation) error - MinItem containers.Optional[btrfsprim.Key] - // Things knowable from the structure of the tree. - MaxItem containers.Optional[btrfsprim.Key] -} - type NodeError[Addr ~int64] struct { Op string NodeAddr Addr @@ -531,40 +519,3 @@ func ReadNode[Addr ~int64](fs diskio.ReaderAt[Addr], sb Superblock, addr Addr, e return node, nil } - -func (exp NodeExpectations) Check(node *Node) error { - var errs derror.MultiError - if exp.LAddr.OK && node.Head.Addr != exp.LAddr.Val { - errs = append(errs, fmt.Errorf("read from laddr=%v but claims to be at laddr=%v", - exp.LAddr.Val, node.Head.Addr)) - } - if exp.Level.OK && node.Head.Level != exp.Level.Val { - errs = append(errs, fmt.Errorf("expected level=%v but claims to be level=%v", - exp.Level.Val, node.Head.Level)) - } - if exp.Generation.OK && node.Head.Generation != exp.Generation.Val { - errs = append(errs, fmt.Errorf("expected generation=%v but claims to be generation=%v", - exp.Generation.Val, node.Head.Generation)) - } - if exp.Owner != nil { - if err := exp.Owner(node.Head.Owner, node.Head.Generation); err != nil { - errs = append(errs, err) - } - } - if node.Head.NumItems == 0 { - errs = append(errs, fmt.Errorf("has no items")) - } else { - if minItem, _ := node.MinItem(); exp.MinItem.OK && exp.MinItem.Val.Compare(minItem) > 0 { - errs = append(errs, fmt.Errorf("expected minItem>=%v but node has minItem=%v", - exp.MinItem, minItem)) - } - if maxItem, _ := node.MaxItem(); exp.MaxItem.OK && exp.MaxItem.Val.Compare(maxItem) < 0 { - errs = append(errs, fmt.Errorf("expected maxItem<=%v but node has maxItem=%v", - exp.MaxItem, maxItem)) - } - } - if len(errs) > 0 { - return errs - } - return nil -} |