From 4649251d68744b9ba9ebbf62cf65f849aac0bfdc Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 17 Sep 2022 00:02:22 -0600 Subject: split files --- lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go (limited to 'lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go') diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go new file mode 100644 index 0000000..f9e4e95 --- /dev/null +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/util.go @@ -0,0 +1,51 @@ +// Copyright (C) 2022 Luke Shumaker +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package rebuildnodes + +import ( + "fmt" + "math" + + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim" + "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect" +) + +func ptrTo[T any](x T) *T { + return &x +} + +func maybeSet[K, V comparable](name string, m map[K]V, k K, v V) error { + if other, conflict := m[k]; conflict && other != v { + return fmt.Errorf("conflict: %s %v can't have both %v and %v", name, k, other, v) + } + m[k] = v + return nil +} + +var maxKey = btrfsprim.Key{ + ObjectID: math.MaxUint64, + ItemType: math.MaxUint8, + Offset: math.MaxUint64, +} + +func keyMm(key btrfsprim.Key) btrfsprim.Key { + switch { + case key.Offset > 0: + key.Offset-- + case key.ItemType > 0: + key.ItemType-- + case key.ObjectID > 0: + key.ObjectID-- + } + return key +} + +func countNodes(nodeScanResults btrfsinspect.ScanDevicesResult) int { + var cnt int + for _, devResults := range nodeScanResults { + cnt += len(devResults.FoundNodes) + } + return cnt +} -- cgit v1.2.3-2-g168b