diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-01 09:56:56 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-01 09:56:56 -0600 |
commit | 437bf733021a6aa3b90042a12a35b887b8ed45a2 (patch) | |
tree | f9215e2d1453dde5691f27069104ccd97eb73da5 /pkg/btrfs/btrfsitem | |
parent | b9fa008b4911a534ff49d191539b57d60cc04663 (diff) |
lib: eagerly unmarshal items
Diffstat (limited to 'pkg/btrfs/btrfsitem')
-rw-r--r-- | pkg/btrfs/btrfsitem/items.go | 21 | ||||
-rw-r--r-- | pkg/btrfs/btrfsitem/items_gen.go | 21 |
2 files changed, 41 insertions, 1 deletions
diff --git a/pkg/btrfs/btrfsitem/items.go b/pkg/btrfs/btrfsitem/items.go index 51e6344..91b7029 100644 --- a/pkg/btrfs/btrfsitem/items.go +++ b/pkg/btrfs/btrfsitem/items.go @@ -1,6 +1,10 @@ package btrfsitem import ( + "fmt" + "reflect" + + "lukeshu.com/btrfs-tools/pkg/binstruct" "lukeshu.com/btrfs-tools/pkg/btrfs/internal" ) @@ -9,3 +13,20 @@ type Type = internal.ItemType type Item interface { isItem() } + +func UnmarshalItem(keytyp Type, dat []byte) (Item, error) { + gotyp, ok := keytype2gotype[keytyp] + if !ok { + return nil, fmt.Errorf("btrfsitem.UnmarshalItem: unknown item type: %v", keytyp) + } + retPtr := reflect.New(gotyp) + n, err := binstruct.Unmarshal(dat, retPtr.Interface()) + if err != nil { + return nil, fmt.Errorf("btrfsitem.UnmarshalItem: %w", err) + } + if n < len(dat) { + return nil, fmt.Errorf("btrfsitem.UnmarshalItem: left over data: got %d bytes but only consumed %d", + len(dat), n) + } + return retPtr.Elem().Interface().(Item), nil +} diff --git a/pkg/btrfs/btrfsitem/items_gen.go b/pkg/btrfs/btrfsitem/items_gen.go index 5234c96..40a0b53 100644 --- a/pkg/btrfs/btrfsitem/items_gen.go +++ b/pkg/btrfs/btrfsitem/items_gen.go @@ -1,6 +1,10 @@ package btrfsitem -import "lukeshu.com/btrfs-tools/pkg/btrfs/internal" +import ( + "reflect" + + "lukeshu.com/btrfs-tools/pkg/btrfs/internal" +) const ( CHUNK_ITEM_KEY = internal.CHUNK_ITEM_KEY @@ -17,6 +21,21 @@ const ( UUID_RECEIVED_SUBVOL_KEY = internal.UUID_RECEIVED_SUBVOL_KEY ) +var keytype2gotype = map[Type]reflect.Type{ + CHUNK_ITEM_KEY: reflect.TypeOf(Chunk{}), + DEV_ITEM_KEY: reflect.TypeOf(Dev{}), + DEV_EXTENT_KEY: reflect.TypeOf(DevExtent{}), + UNTYPED_KEY: reflect.TypeOf(Empty{}), + QGROUP_RELATION_KEY: reflect.TypeOf(Empty{}), + INODE_ITEM_KEY: reflect.TypeOf(Inode{}), + INODE_REF_KEY: reflect.TypeOf(InodeRef{}), + ORPHAN_ITEM_KEY: reflect.TypeOf(Orphan{}), + PERSISTENT_ITEM_KEY: reflect.TypeOf(DevStats{}), + ROOT_ITEM_KEY: reflect.TypeOf(Root{}), + UUID_SUBVOL_KEY: reflect.TypeOf(UUIDMap{}), + UUID_RECEIVED_SUBVOL_KEY: reflect.TypeOf(UUIDMap{}), +} + func (Chunk) isItem() {} func (Dev) isItem() {} func (DevExtent) isItem() {} |