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
|
package btrfsitem
import (
"fmt"
"reflect"
"lukeshu.com/btrfs-tools/pkg/binstruct"
"lukeshu.com/btrfs-tools/pkg/btrfs/internal"
)
type Type = internal.ItemType
type Item interface {
isItem()
}
type Error struct {
Dat []byte
Err error
}
func (Error) isItem() {}
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
}
// Rather than returning a separate error value, return an Error item.
func UnmarshalItem(key internal.Key, dat []byte) Item {
var gotyp reflect.Type
if key.ItemType == UNTYPED_KEY {
var ok bool
gotyp, ok = untypedObjID2gotype[key.ObjectID]
if !ok {
return Error{
Dat: dat,
Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v, ObjectID:%v}, dat): unknown object ID for untyped item",
key.ItemType, key.ObjectID),
}
}
} else {
var ok bool
gotyp, ok = keytype2gotype[key.ItemType]
if !ok {
return Error{
Dat: dat,
Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): unknown item type", key.ItemType),
}
}
}
retPtr := reflect.New(gotyp)
n, err := binstruct.Unmarshal(dat, retPtr.Interface())
if err != nil {
return Error{
Dat: dat,
Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): %w", key.ItemType, err),
}
}
if n < len(dat) {
return Error{
Dat: dat,
Err: fmt.Errorf("btrfsitem.UnmarshalItem({ItemType:%v}, dat): left over data: got %d bytes but only consumed %d",
key.ItemType, len(dat), n),
}
}
return retPtr.Elem().Interface().(Item)
}
|