summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/items.go
blob: a730447d85dc4f7b7ecfbe84d3525d36db64196c (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

// Package btrfsitem contains the definitions of all "items" that may
// be stored in a btrfs tree.
package btrfsitem

import (
	"fmt"
	"reflect"

	"git.lukeshu.com/go/typedsync"

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

type Type = btrfsprim.ItemType

type Item interface {
	isItem()
	Free()
	CloneItem() Item
}

type Error struct {
	Dat []byte
	Err error
}

var errorPool = &typedsync.Pool[*Error]{New: func() *Error { return new(Error) }}

func (*Error) isItem() {}

func (o *Error) Free() {
	*o = Error{}
	errorPool.Put(o)
}

func (o Error) Clone() Error { return o }

func (o *Error) CloneItem() Item {
	ret, _ := errorPool.Get()
	*ret = *o
	return ret
}

func (o Error) MarshalBinary() ([]byte, error) {
	return o.Dat, nil
}

func (o *Error) UnmarshalBinary(dat []byte) (int, error) {
	o.Dat = dat
	return len(dat), nil
}

// UnmarshalItem consumes the byte slice `dat`, unmarshaling it in to
// the item type specified by `key`.
//
// If there is an error, rather than returning a separate error value,
// return an Error item.
func UnmarshalItem(key btrfsprim.Key, csumType btrfssum.CSumType, dat []byte) Item {
	var gotyp reflect.Type
	if key.ItemType == UNTYPED_KEY {
		var ok bool
		gotyp, ok = untypedObjID2gotype[key.ObjectID]
		if !ok {
			ret, _ := errorPool.Get()
			*ret = Error{
				Dat: dat,
				Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v, ObjectID:%v}, dat): unknown object ID for untyped item",
					key.ItemType, key.ObjectID),
			}
			return ret
		}
	} else {
		var ok bool
		gotyp, ok = keytype2gotype[key.ItemType]
		if !ok {
			ret, _ := errorPool.Get()
			*ret = Error{
				Dat: dat,
				Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): unknown item type", key.ItemType),
			}
			return ret
		}
	}
	ptr, _ := gotype2pool[gotyp].Get()
	if csums, ok := ptr.(*ExtentCSum); ok {
		csums.ChecksumSize = csumType.Size()
		csums.Addr = btrfsvol.LogicalAddr(key.Offset)
	}
	n, err := binstruct.Unmarshal(dat, ptr)
	if err != nil {
		ptr.Free()
		ret, _ := errorPool.Get()
		*ret = Error{
			Dat: dat,
			Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): %w", key.ItemType, err),
		}
		return ret
	}
	if n < len(dat) {
		ptr.Free()
		ret, _ := errorPool.Get()
		*ret = Error{
			Dat: dat,
			Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): left over data: got %v bytes but only consumed %v",
				key.ItemType, len(dat), n),
		}
		return ret
	}
	return ptr
}

var bytePool containers.SlicePool[byte]

func cloneBytes(in []byte) []byte {
	out := bytePool.Get(len(in))
	copy(out, in)
	return out
}