summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wanttyp.go
blob: 6c9d72ba864217ae53bbf5dbe0d0c8a14a1fcad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package rebuildtrees

import (
	"context"
	"fmt"

	"github.com/datawire/dlib/dlog"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
	"git.lukeshu.com/btrfs-progs-ng/lib/containers"
)

type wantOffsetType int8

const (
	offsetAny = wantOffsetType(iota)
	offsetExact
	offsetRange
	offsetName
)

type want struct {
	// TODO(lukeshu): Delete the 'want' type in favor of
	// btrfstree.Search.
	ObjectID   btrfsprim.ObjID
	ItemType   btrfsprim.ItemType
	OffsetType wantOffsetType
	OffsetLow  uint64
	OffsetHigh uint64
	OffsetName string
}

func (a want) Compare(b want) int {
	if d := containers.NativeCompare(a.ObjectID, b.ObjectID); d != 0 {
		return d
	}
	if d := containers.NativeCompare(a.ItemType, b.ItemType); d != 0 {
		return d
	}
	if d := containers.NativeCompare(a.OffsetType, b.OffsetType); d != 0 {
		return d
	}
	if d := containers.NativeCompare(a.OffsetLow, b.OffsetLow); d != 0 {
		return d
	}
	if d := containers.NativeCompare(a.OffsetHigh, b.OffsetHigh); d != 0 {
		return d
	}
	if d := containers.NativeCompare(a.OffsetName, b.OffsetName); d != 0 {
		return d
	}
	return 0
}

func (o want) Key() btrfsprim.Key {
	return btrfsprim.Key{
		ObjectID: o.ObjectID,
		ItemType: o.ItemType,
		Offset:   o.OffsetLow,
	}
}

func wantFromKey(k btrfsprim.Key) want {
	return want{
		ObjectID:   k.ObjectID,
		ItemType:   k.ItemType,
		OffsetType: offsetExact,
		OffsetLow:  k.Offset,
	}
}

func (o want) String() string {
	switch o.OffsetType {
	case offsetAny:
		return fmt.Sprintf("{%v %v ?}", o.ObjectID, o.ItemType)
	case offsetExact:
		return fmt.Sprintf("{%v %v %v}", o.ObjectID, o.ItemType, o.OffsetLow)
	case offsetRange:
		return fmt.Sprintf("{%v %v %v-%v}", o.ObjectID, o.ItemType, o.OffsetLow, o.OffsetHigh)
	case offsetName:
		return fmt.Sprintf("{%v %v name=%q}", o.ObjectID, o.ItemType, o.OffsetName)
	default:
		panic(fmt.Errorf("should not happen: OffsetType=%#v", o.OffsetType))
	}
}

type wantWithTree struct {
	TreeID btrfsprim.ObjID
	Key    want
}

func (o wantWithTree) String() string {
	return fmt.Sprintf("tree=%v key=%v", o.TreeID, o.Key)
}

const (
	logFieldItemWant = "btrfs.inspect.rebuild-trees.rebuild.want"
	logFieldTreeWant = "btrfs.util.rebuilt-forrest.add-tree.want"
)

func withWant(ctx context.Context, logField, reason string, wantKey wantWithTree) context.Context {
	ctx = dlog.WithField(ctx, logField+".reason", reason)
	ctx = dlog.WithField(ctx, logField+".key", wantKey)
	return ctx
}