diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-17 18:38:14 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-17 18:38:14 -0400 |
commit | 0f85e72d1331b49b52925d6cc5ad083a0376104c (patch) | |
tree | 9ba66e893d6f66096f6b06284d09c8eb3e50facc /lib/btrfs/btrfstree | |
parent | c0f33186aa7a8903c5e7406024f13fad48cd14e3 (diff) | |
parent | 1ea26f04701fa66e36b058f3efb3a6c7059cdc5c (diff) |
Merge branch 'lukeshu/lint'
Diffstat (limited to 'lib/btrfs/btrfstree')
-rw-r--r-- | lib/btrfs/btrfstree/btree.go | 2 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/btree_tree.go | 5 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/path.go | 34 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/types_node.go | 31 |
4 files changed, 40 insertions, 32 deletions
diff --git a/lib/btrfs/btrfstree/btree.go b/lib/btrfs/btrfstree/btree.go index 4c10ffa..89d4f9d 100644 --- a/lib/btrfs/btrfstree/btree.go +++ b/lib/btrfs/btrfstree/btree.go @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: GPL-2.0-or-later +// Package btrfstree contains core b+-tree implementation and +// interfaces. package btrfstree import ( diff --git a/lib/btrfs/btrfstree/btree_tree.go b/lib/btrfs/btrfstree/btree_tree.go index df58c0c..1e3c789 100644 --- a/lib/btrfs/btrfstree/btree_tree.go +++ b/lib/btrfs/btrfstree/btree_tree.go @@ -188,7 +188,8 @@ func (fs TreeOperatorImpl) treeSearch(treeRoot TreeRoot, fn func(btrfsprim.Key, return nil, nil, err } - if node.Data.Head.Level > 0 { + switch { + case node.Data.Head.Level > 0: // interior node // Search for the right-most node.Data.BodyInterior item for which @@ -220,7 +221,7 @@ func (fs TreeOperatorImpl) treeSearch(treeRoot TreeRoot, fn func(btrfsprim.Key, ToMaxKey: toMaxKey, }) FreeNodeRef(node) - } else { + default: // leaf node // Search for a member of node.Data.BodyLeaf for which diff --git a/lib/btrfs/btrfstree/path.go b/lib/btrfs/btrfstree/path.go index 9b1a5c7..b9ab5bc 100644 --- a/lib/btrfs/btrfstree/path.go +++ b/lib/btrfs/btrfstree/path.go @@ -101,23 +101,22 @@ func (elem TreePathElem) writeNodeTo(w io.Writer) { func (path TreePath) String() string { if len(path) == 0 { return "(empty-path)" + } + var ret strings.Builder + fmt.Fprintf(&ret, "%s->", path[0].FromTree.Format(btrfsprim.ROOT_TREE_OBJECTID)) + if len(path) == 1 && path[0] == (TreePathElem{FromTree: path[0].FromTree, FromItemSlot: -1}) { + ret.WriteString("(empty-path)") } else { - var ret strings.Builder - fmt.Fprintf(&ret, "%s->", path[0].FromTree.Format(btrfsprim.ROOT_TREE_OBJECTID)) - if len(path) == 1 && path[0] == (TreePathElem{FromTree: path[0].FromTree, FromItemSlot: -1}) { - ret.WriteString("(empty-path)") - } else { - path[0].writeNodeTo(&ret) - } - for _, elem := range path[1:] { - fmt.Fprintf(&ret, "[%v]", elem.FromItemSlot) - if elem.ToNodeAddr != 0 { - ret.WriteString("->") - elem.writeNodeTo(&ret) - } + path[0].writeNodeTo(&ret) + } + for _, elem := range path[1:] { + fmt.Fprintf(&ret, "[%v]", elem.FromItemSlot) + if elem.ToNodeAddr != 0 { + ret.WriteString("->") + elem.writeNodeTo(&ret) } - return ret.String() } + return ret.String() } func (path TreePath) DeepCopy() TreePath { @@ -128,9 +127,10 @@ func (path TreePath) Parent() TreePath { return path[:len(path)-1] } -// path.Node(x) is like &path[x], but negative values of x move down -// from the end of path (similar to how lists work in many other -// languages, such as Python). +// Node is returns an element from the path; `path.Node(x)` is like +// `&path[x]`, but negative values of x move down from the end of path +// (similar to how lists work in many other languages, such as +// Python). func (path TreePath) Node(x int) *TreePathElem { if x < 0 { x += len(path) diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go index 150539d..8295ccb 100644 --- a/lib/btrfs/btrfstree/types_node.go +++ b/lib/btrfs/btrfstree/types_node.go @@ -113,20 +113,22 @@ type NodeHeader struct { // .Head.NumItems. func (node Node) MaxItems() uint32 { bodyBytes := node.Size - uint32(nodeHeaderSize) - if node.Head.Level > 0 { + switch { + case node.Head.Level > 0: return bodyBytes / uint32(keyPointerSize) - } else { + default: return bodyBytes / uint32(itemHeaderSize) } } func (node Node) MinItem() (btrfsprim.Key, bool) { - if node.Head.Level > 0 { + switch { + case node.Head.Level > 0: if len(node.BodyInterior) == 0 { return btrfsprim.Key{}, false } return node.BodyInterior[0].Key, true - } else { + default: if len(node.BodyLeaf) == 0 { return btrfsprim.Key{}, false } @@ -135,12 +137,13 @@ func (node Node) MinItem() (btrfsprim.Key, bool) { } func (node Node) MaxItem() (btrfsprim.Key, bool) { - if node.Head.Level > 0 { + switch { + case node.Head.Level > 0: if len(node.BodyInterior) == 0 { return btrfsprim.Key{}, false } return node.BodyInterior[len(node.BodyInterior)-1].Key, true - } else { + default: if len(node.BodyLeaf) == 0 { return btrfsprim.Key{}, false } @@ -221,15 +224,15 @@ func (node Node) MarshalBinary() ([]byte, error) { buf := make([]byte, node.Size) - if bs, err := binstruct.Marshal(node.Head); err != nil { + bs, err := binstruct.Marshal(node.Head) + if err != nil { return buf, err - } else { - if len(bs) != nodeHeaderSize { - return nil, fmt.Errorf("header is %v bytes but expected %v", - len(bs), nodeHeaderSize) - } - copy(buf, bs) } + if len(bs) != nodeHeaderSize { + return nil, fmt.Errorf("header is %v bytes but expected %v", + len(bs), nodeHeaderSize) + } + copy(buf, bs) if node.Head.Level > 0 { if err := node.marshalInteriorTo(buf[nodeHeaderSize:]); err != nil { @@ -456,6 +459,8 @@ func newNodeRef[Addr ~int64]() *diskio.Ref[Addr, Node] { return (*diskio.Ref[Addr, Node])(unsafe.Pointer(ret)) //nolint:gosec // I know it's unsafe. } +// ReadNode reads a node from the given file. +// // It is possible that both a non-nil diskio.Ref and an error are // returned. The error returned (if non-nil) is always of type // *NodeError[Addr]. Notable errors that may be inside of the |