From 2f28e21aef9be5755fa5f175bb74c9aa6548d1cc Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 12 Jul 2022 16:23:43 -0600 Subject: lib/btrfs: Don't have errors stutter Since ad9ac6d07ce1819260c2b7f090fd4fe742c80d9f binstruct itself wraps all errors with the type and method; no need for the Node and SysChunk .MarshalBinary and .UnmarshalBinary methods to also prefix the errors they emit with that info. --- lib/btrfs/types_node.go | 19 ++++++++++--------- lib/btrfs/types_superblock.go | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/btrfs/types_node.go b/lib/btrfs/types_node.go index 0b5bc5b..9f20ea9 100644 --- a/lib/btrfs/types_node.go +++ b/lib/btrfs/types_node.go @@ -130,23 +130,23 @@ func (node *Node) UnmarshalBinary(nodeBuf []byte) (int, error) { } n, err := binstruct.Unmarshal(nodeBuf, &node.Head) if err != nil { - return n, fmt.Errorf("btrfs.Node.UnmarshalBinary: %w", err) + return n, err } if node.Head.Level > 0 { _n, err := node.unmarshalInternal(nodeBuf[n:]) n += _n if err != nil { - return n, fmt.Errorf("btrfs.Node.UnmarshalBinary: internal: %w", err) + return n, fmt.Errorf("internal: %w", err) } } else { _n, err := node.unmarshalLeaf(nodeBuf[n:]) n += _n if err != nil { - return n, fmt.Errorf("btrfs.Node.UnmarshalBinary: leaf: %w", err) + return n, fmt.Errorf("leaf: %w", err) } } if n != len(nodeBuf) { - return n, fmt.Errorf("btrfs.Node.UnmarshalBinary: left over data: got %v bytes but only consumed %v", + return n, fmt.Errorf("left over data: got %v bytes but only consumed %v", len(nodeBuf), n) } return n, nil @@ -154,11 +154,12 @@ func (node *Node) UnmarshalBinary(nodeBuf []byte) (int, error) { func (node Node) MarshalBinary() ([]byte, error) { if node.Size == 0 { - return nil, fmt.Errorf("btrfs.Node.MarshalBinary: .Size must be set") + return nil, fmt.Errorf(".Size must be set") } if node.Size <= uint32(binstruct.StaticSize(NodeHeader{})) { - return nil, fmt.Errorf("btrfs.Node.MarshalBinary: .Size must be greater than %v", - binstruct.StaticSize(NodeHeader{})) + return nil, fmt.Errorf(".Size must be greater than %v, but is %v", + binstruct.StaticSize(NodeHeader{}), + node.Size) } if node.Head.Level > 0 { node.Head.NumItems = uint32(len(node.BodyInternal)) @@ -171,7 +172,7 @@ func (node Node) MarshalBinary() ([]byte, error) { if bs, err := binstruct.Marshal(node.Head); err != nil { return buf, err } else if len(bs) != binstruct.StaticSize(NodeHeader{}) { - return nil, fmt.Errorf("btrfs.Node.MarshalBinary: header is %v bytes but expected %v", + return nil, fmt.Errorf("header is %v bytes but expected %v", len(bs), binstruct.StaticSize(NodeHeader{})) } else { copy(buf, bs) @@ -378,7 +379,7 @@ func ReadNode[Addr ~int64](fs util.File[Addr], sb Superblock, addr Addr, laddrCB // parse (main) - if _, err := nodeRef.Data.UnmarshalBinary(nodeBuf); err != nil { + if _, err := binstruct.Unmarshal(nodeBuf, &nodeRef.Data); err != nil { return nodeRef, fmt.Errorf("btrfs.ReadNode: node@%v: %w", addr, err) } diff --git a/lib/btrfs/types_superblock.go b/lib/btrfs/types_superblock.go index a407501..30adb86 100644 --- a/lib/btrfs/types_superblock.go +++ b/lib/btrfs/types_superblock.go @@ -121,12 +121,12 @@ type SysChunk struct { func (sc SysChunk) MarshalBinary() ([]byte, error) { dat, err := binstruct.Marshal(sc.Key) if err != nil { - return dat, fmt.Errorf("%T.MarshalBinary: %w", sc, err) + return dat, err } _dat, err := binstruct.Marshal(sc.Chunk) dat = append(dat, _dat...) if err != nil { - return dat, fmt.Errorf("%T.MarshalBinary: %w", sc, err) + return dat, err } return dat, nil } @@ -134,12 +134,12 @@ func (sc SysChunk) MarshalBinary() ([]byte, error) { func (sc *SysChunk) UnmarshalBinary(dat []byte) (int, error) { n, err := binstruct.Unmarshal(dat, &sc.Key) if err != nil { - return n, fmt.Errorf("%T.UnmarshalBinary: %w", *sc, err) + return n, err } _n, err := binstruct.Unmarshal(dat[n:], &sc.Chunk) n += _n if err != nil { - return n, fmt.Errorf("%T.UnmarshalBinary: %w", *sc, err) + return n, err } return n, nil } -- cgit v1.2.3-2-g168b